AngularJS - 呈现包含在字符串中的 HTML 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17826758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:27:37  来源:igfitidea点击:

AngularJS - Render HTML tags that are contained in a string

javascripthtmlangularjsescaping

提问by JVG

My database stores product information, and a lot of this is organised into lists. I load the data into Angular as $scope.post.

我的数据库存储产品信息,其中很多信息都组织成列表。我将数据加载到 Angular 中$scope.post

For instance,

例如,

$scope.post.size_description = '<li> Fits true to size. Take your normal size\r</li>
   <li> Slim-cut, mid-rise style</li>
   <li> Long in length, alter to fit</li>
   <li> Model wears an IT 48\r</li>
   <li> Model measures: waist size 32, height 6\'1"/ 185cm\r</li>'.

When I try to load this data into my Angular app, it gets rendered as text (i.e. the <li>are not parsed). I understand this probably happens for security reasons, but is there any way around it?

当我尝试将此数据加载到我的 Angular 应用程序中时,它被呈现为文本(即<li>未解析)。我知道这可能是出于安全原因,但有什么办法可以解决吗?

采纳答案by mnsr

As Damax has said here: https://stackoverflow.com/a/11640420/769083

正如 Damax 在这里所说:https://stackoverflow.com/a/11640420/769083

<div ng-bind-html-unsafe="post.size_description"></div>

回答by Andy

ngBindHtml worked for me. See more in the docs here: https://docs.angularjs.org/api/ng/directive/ngBindHtml

ngBindHtml 为我工作。在此处的文档中查看更多信息:https: //docs.angularjs.org/api/ng/directive/ngBindHtml

<div ng-bind-html="post.size_description"></div>