我查找了有关如何在 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/