一尘不染

Angular JS在标记中显示HTML

angularjs

我试图使用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="&lt;a class=&quot;action_hrefs full-width bgcolor10 purple-hover flat-button flat-white-button btn&quot; data-value=&quot;947&quot; href=&quot;#&quot;&gt;&lt;i class=&quot;fa fa-lock&quot;&gt;&lt;/i&gt;&nbsp;Unlock&lt;/a&gt;"></div>

阅读 217

收藏
2020-07-04

共1个答案

一尘不染

确定,我找到了解决方案:

JS:

$scope.renderHtml = function(html_code)
{
    return $sce.trustAsHtml(html_code);
};

HTML:

<p ng-bind-html="renderHtml(value.button)"></p>
2020-07-04