我试图使用ng-bind-html-unsafe属性在模板内插入HTML 。但是由于某种原因,它不起作用。
ng-bind-html-unsafe
我的代码:
<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,但在属性中显示如下内容:
ng-bind-html-unsafe="value.button"
ng-bind-html- unsafe="{{value.button}}"
<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>
确定,我找到了解决方案:
JS:
$scope.renderHtml = function(html_code) { return $sce.trustAsHtml(html_code); };
HTML:
<p ng-bind-html="renderHtml(value.button)"></p>