在Angular应用中使用HTML的样式JSON



我正在用JSON对象将文本注入我的Angular应用程序。有一个"生物"部分,需要诸如<br />之类的HTML样式,因此文本不是一块不可读的文本。我尝试过trustAsHtml,但对我不起作用。一个工作的例子是我通过Angular文档阅读的,并且无法让我为我工作时最好。

帮助您非常感谢!

html w/angular js

<div class="bio">
    {{exhibits[whichItem].bio}}
</div>

JSON

[
  {
    "name":"Name goes here",
    "bio":"First long block of text goes here then it needs a break <br /> and the second long block of text is here."
  }
]

您必须将'ngsanitize'模块添加为依赖项另外,您必须包括相应的脚本:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-sanitize.js"></script>

然后使用ng-bind-html

<div class="bio" ng-bind-html="exhibits[whichItem].bio">
</div>

如果您不使用ngSanitize模块,它将给您ng-bind-html

的错误

您可以在此处参考

这个

<div class="bio"ng-bind-html=exhibits[whichItem].bio>
</div>

可能会解决这个问题。

最新更新