从AngularJS对象渲染html标记

时间:2016-03-02 00:44:45

标签: javascript html angularjs

我已经查找了关于如何在angularJS中呈现html标签的类似问题但是没有一个对我有效,因为我正在使用ng-repeat。任何人都有任何建议。

     <div ng-repeat="o in SurveyResults.SurveyQuestions">
                    {{o.SurveyQuestionId}}
                    {{o.QuestionText}}
     </div>

我的o.QuestionText包含<b>How well do we deliver on our promises?</b> <br />Consider our responsiveness to your requests, ability to meet deadlines and <i>accountability for the outcomes</i>.

等数据

它显示标签而不渲染标签。

我确实尝试将其放入我的JS文件

    homePage.directive('htmlSafe', function($parse, $sce) {
    return {
        link: function(scope, elem, attr) {
            var html = attr.ngBindHtml;
            if(angular.isDefined(html)) {
                var getter = $parse(html);
                var setter = getter.assign;
                setter(scope, $sce.trustAsHtml(getter(scope)));
            }
        }
    };
});

然后在重复中设置html bind

 <div ng-repeat-html="o in SurveyResults.SurveyQuestions">
                    {{o.SurveyQuestionId}}
                    {{o.QuestionText}}
     </div>

还有其他建议吗?

1 个答案:

答案 0 :(得分:1)

只需使用ng-bind-html,就不需要新的指令

 <div ng-repeat="o in SurveyResults.SurveyQuestions">
                {{o.SurveyQuestionId}}
                <span ng-bind-html="o.QuestionText"></span>
 </div>