AngularJS - Is it possible to use ng-repeat to ren

2019-01-23 13:34发布

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.

Is it possible to use ng-repeat to render HTML property?

I've created this jsFiddle to demonstrate my issue. http://jsfiddle.net/DrtNc/1/

3条回答
我命由我不由天
2楼-- · 2019-01-23 14:14

item.html will always be interpreted as text. you have to convert it to html explicitly. click here

I have added a render function which will convert each string to html.

查看更多
时光不老,我们不散
3楼-- · 2019-01-23 14:15

I think using ng-bind-html-unsafe will get you what you need.

<div ng:repeat="item in items" ng-bind-html-unsafe="item.html"></div>

Here's a working fiddle: http://jsfiddle.net/nfreitas/aHfAp/

Documentation for the directive can be found here: http://docs.angularjs.org/api/ng.directive:ngBindHtmlUnsafe

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-23 14:25

The way I achieved this is by ng-bind-html inside the ng-repeat;

<div ng-repeat="comment in comments">
  <div ng-bind-html="comment.content"></div>
</div>

Hope this helps someone!

查看更多
登录 后发表回答