use*_*661 3 html javascript json angularjs
我做了一个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.
Run Code Online (Sandbox Code Playgroud)
我使用此字符串填充我的网页上的div,使其显示如下:
<div class="lyrics-container>{{ the text in the JSON string }} </div>
Run Code Online (Sandbox Code Playgroud)
但是,当使用该文本填充div时,我会得到确切的字符串,这意味着<br>s显示为文本.我希望他们实际执行他们的功能并打破界限.有没有办法强制浏览器在字符串中解释HTML?
我正在使用Angular来获取数据并填充div,如果这有任何区别的话.
这不是直截了当的.你需要使用ngBindHtml.
$scope.content = "<b>this is bold content</b>";
Run Code Online (Sandbox Code Playgroud)
<div ng-bind-html="content"></div>
Run Code Online (Sandbox Code Playgroud)
您需要以下模块:
http://code.angularjs.org/1.0.3/angular-sanitize.js
一定要声明ngSanitize为模块依赖,如下所示:
var app = angular.module('angularjs-starter', ["ngSanitize"]);
Run Code Online (Sandbox Code Playgroud)