javascript - 从 AngularJS 对象渲染 html 标签

标签 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>

还有其他建议吗?

最佳答案

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

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

关于javascript - 从 AngularJS 对象渲染 html 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35736439/

相关文章:

html - Sublime 2,如何自动关闭 HTML 标签并将光标放在标签内

javascript - jQuery 复选框选择和更改类

javascript - ExtJS 和/或 DHTML 中的事件的 "related target"是什么?

javascript - 在 jquery 中使用选项卡

html - CSS/HTML 加载网页时出现水平滚动条

javascript - HTML Form 在不离开当前页面的情况下向 PHP Form 发送数据

javascript - JQuery - 如何处理可以通过箭头键更新的字段?

javascript - 将 ng-model 的一部分与 angularjs 中的范围绑定(bind)

angularjs - 输入时未验证 Angular ui-bootstrap datepicker 思维

javascript - 检测 AngularJS 中异步文件上传的进度