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

JVG*_*JVG 15 html javascript escaping angularjs

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

例如,

$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>'.
Run Code Online (Sandbox Code Playgroud)

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

jzm*_*jzm 12

正如Damax在此处所说:https://stackoverflow.com/a/11640420/769083

<div ng-bind-html-unsafe="post.size_description"></div>
Run Code Online (Sandbox Code Playgroud)

  • 自Angular 1.2以来,ng-bind-html-unsafe已被折旧.见http://stackoverflow.com/questions/9381926/insert-html-into-view-using-angularjs (6认同)

And*_*ndy 5

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

<div ng-bind-html="post.size_description"></div>
Run Code Online (Sandbox Code Playgroud)