我有一个角指令:Angular指令呈现HTML标记,我该如何阻止它?
<some-dir text="{{characterDescription}}"></some-dir>
app.directive('someDir', function() {
"use strict";
return {
restrict: 'E',
transclude: false,
// replace: true,
template: '<p></p>',
scope: {
text: '@'
},
link: function(scope, elem) {
elem.append(scope.text);
}
};
});
被传递到它的文字是这样的:
{
characterDescription : <b>tall</b>
}
目前,它的渲染HTML,所以我看到高大 ,但我希望它将HTML视为纯文本,所以我想看看<b>tall</b>
。
有没有在我的指令中完成这一点的角度方式?
请看这里的示例:https://docs.angularjs.org/api/ngSanitize/service/$sanitize –
您是否尝试过elem.text(scope.text)? 这不会将字符串解释为HTML,而是纯粹作为字符串。 http://api.jquery.com/text/#text2 – pcagica
如果我理解你的问题,[这个答案](http://stackoverflow.com/questions/6817262/how-to-display-html-tags- as-plain-text)可能会有所帮助。 –