我遇到了angularJs的问题。我的应用程序从服务器请求一些数据,并且从服务器返回的数据中的一个值是一串html。我在像我这样的角度模板中绑定它
<div>{{{item.location_icons}}</div>
但正如您所料,我看到的不是图标图像而是标记 基本上div中的东西看起来像
"<i class='my-icon-class'/>"
这不是我想要的。
任何人都知道我可以做什么来解析翻译中的html
答案 0 :(得分:39)
您希望将ng-bind-html
和ng-bind-html-unsafe
用于此目的。
示例显示为here
答案 1 :(得分:18)
由于不推荐使用ng-bind-html-unsafe,您可以改用此代码。
您需要在控制器内创建功能:
$scope.toTrustedHTML = function( html ){
return $sce.trustAsHtml( html );
}
并在您的视图中使用类似的内容:
<span ng-bind-html='toTrustedHTML( myHTMLstring )'></span>
别忘了注入$ sce:
AppObj.controller('MyController', function($scope, $sce) {
//your code here
});
答案 2 :(得分:8)
更好的方法是使用$compile
代替ng-bind-html-unsafe
。
请参阅:http://docs.angularjs.org/api/ng.$compile
最后,目前,angularJS的最新版本(发布候选版本1.2.0)没有任何ng-bind-html-unsafe
指令。因此,我非常鼓励您在将来更新应用之前考虑此选项。 (ng-bind-html-unsafe
可能/将不再工作......)
http://code.angularjs.org/1.2.0rc1/docs/api/ng.directive:ngBindHtml