Javascript AngularJS - 是否可以使用 ng-repeat 来呈现 HTML 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11408668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS - Is it possible to use ng-repeat to render HTML values?
提问by Shlomi Schwartz
I'm using AngularJS with a third party service that generates html responses. I want to use ng-repeat to render the HTML responses as a list, however Angular renders it as text.
我将 AngularJS 与生成 html 响应的第三方服务一起使用。我想使用 ng-repeat 将 HTML 响应呈现为列表,但是 Angular 将其呈现为文本。
Is it possible to use ng-repeat to render HTML property?
是否可以使用 ng-repeat 来呈现 HTML 属性?
I've created this jsFiddle to demonstrate my issue. http://jsfiddle.net/DrtNc/1/
我创建了这个 jsFiddle 来演示我的问题。 http://jsfiddle.net/DrtNc/1/
回答by Noah Freitas
I think using ng-bind-html-unsafe
will get you what you need.
我认为使用ng-bind-html-unsafe
会得到你需要的东西。
<div ng:repeat="item in items" ng-bind-html-unsafe="item.html"></div>
Here's a working fiddle: http://jsfiddle.net/nfreitas/aHfAp/
这是一个工作小提琴:http: //jsfiddle.net/nfreitas/aHfAp/
Documentation for the directive can be found here: http://docs.angularjs.org/api/ng.directive:ngBindHtmlUnsafe
该指令的文档可以在这里找到:http: //docs.angularjs.org/api/ng.directive: ngBindHtmlUnsafe
回答by Matt Saunders
The way I achieved this is by ng-bind-html inside the ng-repeat;
我实现这一点的方法是通过 ng-repeat 中的 ng-bind-html;
<div ng-repeat="comment in comments">
<div ng-bind-html="comment.content"></div>
</div>
Hope this helps someone!
希望这可以帮助某人!
回答by Kishore
item.html will always be interpreted as text. you have to convert it to html explicitly. click here
item.html 将始终被解释为文本。您必须将其显式转换为 html。点击这里
I have added a render function which will convert each string to html.
我添加了一个渲染函数,它将每个字符串转换为 html。