?
本文檔使用 php中文網(wǎng)手冊(cè) 發(fā)布
創(chuàng)建一個(gè)綁定,使用安全的方式使當(dāng)前元素的innerHTML為 expression
的計(jì)算結(jié)果。默認(rèn)情況下,innerHTML中的內(nèi)容會(huì)使用 $sanitize 服務(wù)進(jìn)行凈化。為使用這個(gè)功能,確保 $sanitize
有效,例如,包含 ngSanitize
到你的模塊依賴中 (它不在 Angular核心中)。如果你知道值是安全的,你也可以忽略凈化。如要這樣做,需要通過(guò) $sce.trustAsHtml來(lái)綁定一個(gè)明確可信的值。 請(qǐng)參見(jiàn)Strict Contextual Escaping (SCE)下的例子。
注意:如果 $sanitize
服務(wù)無(wú)效,并且綁定的值不是明確可信的,你將得到一個(gè)錯(cuò)誤(而不會(huì)使用它)。
<ANY
ng-bind-html="">
...
</ANY>
參數(shù) | 類型 | 詳述 |
---|---|---|
ngBindHtml | expression | 可求值表達(dá)式。 |
試下這里: 在文本框輸入文本并查看問(wèn)候信息的變化。
<div ng-controller="ExampleController">
<p ng-bind-html="myHTML"></p>
</div>
script.jsangular.module('bindHtmlExample', ['ngSanitize'])
.controller('ExampleController', ['$scope', Function($scope) {
$scope.myHTML =
'I am an <code>HTML</code>string with ' +
'<a href="#">links!</a> and other <em>stuff</em>';
}]);
protractor.jsit('should check ng-bind-html', Function() {
expect(element(by.binding('myHTML')).getText()).toBe(
'I am an HTMLstring with links! and other stuff');});