$ sce.trustAsHtml无法正常工作 [英] $sce.trustAsHtml not working
问题描述
好的,所以下面我在代码中测试过的内容可以起作用:
Alright, so the following below that I've tested in code works:
javascript:
var string = '<p>hello</p>';
$scope.html = $sce.trustAsHtml(string);
html:
<span ng-bind-html="html"></span>
我真正想做的事情是这样的:以JSON形式从服务器中提取一堆项目. JSON的键之一称为描述",它只是一长串html代码.
The thing I'm really trying to do is this: Pulling a bunch of items from the server in JSON form. One of the JSON's key is called "description", which is just a long string of html code.
我将这些项目推送到$ scope数组中,以便可以通过ng-repeat指令在页面上显示它们.下面的代码总结了它的完成方式:
I'm pushing these items into a $scope array so I can show them on the page through an ng-repeat directive. The code below summarizes how it's being done:
javascript:
$.getJSON('someURL', function(data) {
$scope.items = [];
for (var i = 0; i < data.count; i++) {
var item = {};
item.description = $sce.trustAsHtml(data.item[i].description);
$scope.items.push(item);
}
});
html:
<p ng-repeat="item in items">
<span ng-bind-html="item.description"></div>
</p>
由于某种原因,这没有产生任何输出.我读到的东西是,将$ sce.trustAsHtml()绑定到的任何变量都必须是$ scope变量.在这种情况下,我将其设置为常规变量"item.description",然后将其添加到$ scope.item数组中.
This isn't producing any output for some reason. Something I read was that whatever variable you bind the $sce.trustAsHtml() to must be a $scope variable. In this case, I'm setting it to a regular variable "item.description" and then adding it to a $scope.item array.
我怀疑这可能是它不起作用的原因,但是我不知道如何解决它.
I suspect this may be why it's not working, but I don't know how to go about fixing it.
有人可以帮忙吗?
谢谢!
推荐答案
尝试<span ng-bind-html="'{{item.description}}'"></span>
.
类似的事情对我有用.
Something like this worked for me.
这篇关于$ sce.trustAsHtml无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!