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

2019-01-23 14:02发布

问题:

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/

回答1:

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



回答2:

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!



回答3:

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.