我有一些控制器包含$ scope值,它是html字符串和一个变量

$scope.variableText="value of variable";
$scope.htmlString = "<b>HP LaserJet 1020</b> printer provides ________________ dpi printing. {{variableText}}";


在视图中使用的DOM元素

<span><ANY ng-bind-html="htmlString | unsafe"></ANY></span>


出于信任,因为html我已经使用了此过滤器功能,

app.filter('unsafe',function($sce)
{
     return $sce.trustAsHtml;
});


它可以成功地将结果作为HTML输出,但不会解析{{variableText}}

HP LaserJet 1020打印机提供________________ dpi打印。 {{variableText}}

最佳答案

试着做

的HTML

<span><ANY ng-bind-html="htmlString | unsafe:this"></ANY></span>


JS

app.filter('unsafe', ['$sce', '$compile', function ($sce, $compile) {
return function (input, scope) {

    //create an angular element. (this is still our "view")
    var el = angular.element(input);

    //compile the view into a function.
    var compiled = $compile(el);

    //bind our view to the scope!
    //(try commenting out this line to see what happens!)
    compiled(scope);

    return $sce.trustAsHtml(el);
};}]);

10-05 21:05
查看更多