角 - 插值字符串与HTML [英] Angular - Interpolate string with html
问题描述
所以,我已经有了一个模板字符串看起来是这样的:
So i've got a "template string" that looks like this:
var templateString = "Hello my name is {{name}}";
这是我要插补的名字是一个变量。所以我继续这样的:
The name that I want to interpolate is a in variable. So I proceeded this way:
var miniScope = {
name: "Chuck"
};
var sentence = $interpolate(templateString)(miniScope);
/* sentence: "Hello my name is Chuck" */
这工作。现在我想以粗体的名称。我明明试过:
This works. Now I'd like to bold the name. I've obviously tried:
var miniScope = {
name: "<strong>Chuck</strong>"
};
但是HTML code被逃脱。任何想法我怎么能做到这一点?
But the html code gets escaped. Any idea how I can achieve this?
PS:对于那些你们谁不知道为什么我不只是把字符串中的模板,那是因为我的模板字符串来自服务器
PS: For those of you who wonder why I don't just put the string in the template, it's because my template string is coming from the server.
推荐答案
这 Plunkr 输出你好,我的名字叫查克作为预期。 JavaScript的距离问题保持不变。
This Plunkr outputs "Hello my name is Chuck" as expected. The JavaScript is unchanged from the question.
var app = angular.module("app", ["ngSanitize"]);
app.controller("TestCtrl", TestCtrl);
function TestCtrl($scope, $interpolate) {
var templateString = "Hello my name is {{name}}";
var miniScope = {
name: "<strong>Chuck</strong>"
};
$scope.sentence = $interpolate(templateString)(miniScope);
}
和在HTML中,使用您使用 NG-绑定-HTML
来保持HTML被连接codeD。
And in your HTML, make use you use ng-bind-html
to keep the HTML from being encoded.
<body ng-controller="TestCtrl">
<div ng-bind-html="sentence"></div>
</body>
这篇关于角 - 插值字符串与HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!