3

我进行了一个 AJAX 调用,它在 JSON 对象中为我提供了一个字符串,其中包含如下格式的文本:

Your girl was in Berkeley with a communist reader. <br> Mine was entombed within boombox and walkman. <br> I was a hoarder, but girl, that was back then. <br> The gloves are off, the wisdom teeth are out. <br> What you on about? <br> I can feel it in my bones. <br> I can feel it in my bones. <br> I'm stronger now, I'm ready for the house. <br> Such a modest mouse. <br> I can't do this alone.

我使用这个字符串在我的网页上填充一个 div,它看起来像这样:

<div class="lyrics-container>{{ the text in the JSON string }} </div>

但是,当用该文本填充 div 时,我得到了确切的字符串,这意味着<br>s 显示为文本。我希望他们真正发挥作用并打破界限。有没有办法强制浏览器在字符串中解释 HTML?

我正在使用 Angular 来获取数据并填充 div,如果这有什么不同的话。

4

2 回答 2

6

这不是直截了当的。你需要使用ngBindHtml.

控制器

$scope.content = "<b>this is bold content</b>";

HTML

<div ng-bind-html="content"></div>

您将需要以下模块:

http://code.angularjs.org/1.0.3/angular-sanitize.js

请务必声明ngSanitize为模块依赖项,如下所示:

var app = angular.module('angularjs-starter', ["ngSanitize"]);
于 2013-08-15T03:33:11.210 回答
4

您可以使用ng-bind-html-unsafe

<span ng-bind-html-unsafe="content"></span>

Demo

于 2013-08-15T03:46:11.367 回答