AngularJS ng-bind-html

时间:2014-12-08 14:40:00

标签: javascript html angularjs

这是我在Angular中的过滤器,我只显示了应用程序的片段。这适用于一个控制器,但当我尝试在另一个控制器中使用它时,它不会。

我的意思是不起作用 - 它不会在DOM内部呈现。它虽然在标签内部呈现。

AngularJS: <h2 ng-bind-html="'{{ answer.title }}' | to_trusted"></h2>

示例已渲染 <h2 ng-bind-html="'0-60 minutes per week' | to_trusted" class="ng-binding"></h2>

AngularJS过滤器

    .filter('to_trusted', ['$sce', function($sce){
        return function(text) {
            return $sce.trustAsHtml(text);
        };
    }])

我在这里偶然错过了什么吗?

1 个答案:

答案 0 :(得分:1)

我认为这对你有用的是这种语法,因为answer.title已经是文本了:

<h2 ng-bind-html="answer.title | to_trusted"></h2>