gpt4 book ai didi

angularjs - 在AngularJS中的ng-repeat中输出HTML

转载 作者:行者123 更新时间:2023-12-04 04:54:48 24 4
gpt4 key购买 nike

我的 $scope包含以下 items

$scope.items= [
{ body : '<span> value </span>' },
{ body : '<span> whatever </span>' }
];

当我尝试
<li ng-repeat="item in items">{{item.body}}</li>

HTML 输出为:
<li>&lt;span&gt; value &lt;/span&gt;</li>
<li>&lt;span&gt; whatever &lt;/span&gt;</li>

然后浏览器上出现的文字是这样的:
<span> value </span>
<span> whatever </span>

如何转义跨度并使它们被解析为 HTML?

编辑:已接受答案中的链接不再适用于最新版本。目前 this is how you do it

最佳答案

如果您绝对确定您正在创建的代码不能包含来自用户数据的任何漏洞,请查看 ngBindHtmlUnsafe:

http://docs.angularjs.org/api/ng.directive:ngBindHtmlUnsafe

如果您需要清理可能有危险的 html 片段,请查看 Sanitize 模块中的 ngBindHtml:

http://docs.angularjs.org/api/ngSanitize.directive:ngBindHtml

关于angularjs - 在AngularJS中的ng-repeat中输出HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16902861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com