我试图使用ng-bind-html-unsafe属性在模板中插入HTML。但由于某种原因它不工作。
我的代码:
<tr class="white two-button" ng-repeat="(key,value) in recommendations | ojoscoreFilter:ojoscore | relevancyscoreFilter:relevancyscore | orderBy:predicate:reverse"> <td> <div ng-bind-html-unsafe="value.button"></div> </td> </tr>
我无法看到HTML。
如果我将ng-bind-html-unsafe =“value.button”更改为ng-bind-html-unsafe =“{{value.button}}”,那么它会显示HTML,但是在属性中,
<div ng-bind-html-unsafe="<a class="action_hrefs full-width bgcolor10 purple-hover flat-button flat-white-button btn" data-value="947" href="#"><i class="fa fa-lock"></i> Unlock</a>"></div>
Ok我找到了解决方案:
JS:
$scope.renderHtml = function(html_code) { return $sce.trustAsHtml(html_code); };
HTML:
<p ng-bind-html="renderHtml(value.button)"></p>