简体   繁体   English

AngularJS HTML标签显示

[英]Angularjs html tag is showing

I am trying to add the following code. 我正在尝试添加以下代码。

$scope.journals = [
   {title: "Journal", content: "This is content <br>"}
]

The HTML that I have is: 我拥有的HTML是:

<div ng-repeat="journal in journals">
   <h1>{{journal.title}}</h1>
   <span>{{journal.content}}</span>
</div>

However the "br" Tag is showing as just plain text and not html. 但是,“ br”标记仅显示为纯文本,而不显示为html。 How can I fix that? 我该如何解决?

if you don't want to mess with $sce and you just want to get er done: 如果您不想惹$ sce,而只想完成er:

lrApp.directive('bindHtml', function () {
return {
    restrict: 'A',
    link: function (scope, elem, attrs) {
        scope.$watch(attrs.bindHtml,function(nv,ov){
            elem.html(nv);
        });
    }
};
});

Not sure if this can work for you but you can use a combination of ng-bind-html with $sce.trustAsHtml() to mark your content as trusted. 不知道这是否适合您,但是您可以结合使用ng-bind-html$sce.trustAsHtml()将内容标记为受信任。 Note that you'll only want to do this if you can truly consider the content as safe. 请注意,只有在您可以真正认为内容安全时,才需要这样做。

Here's a plunk as an example: 这里有一个小例子:

http://plnkr.co/edit/L51T4OxdF8AFF5lRQdQW http://plnkr.co/edit/L51T4OxdF8AFF5lRQdQW

Here's the $sce documentation. 这是$sce文档。 Worth a read IMHO to understand the impact of trustAs . 值得一读的恕我直言,以了解trustAs的影响。

https://docs.angularjs.org/api/ng/service/ $sce https://docs.angularjs.org/api/ng/service/ $ sce

您只需要使用由angularjs提供的ng-bind-html指令

<span ng-bind-html="journal.content"></span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM