?
This document uses PHP Chinese website manual Release
創(chuàng)建一個綁定,使用安全的方式使當前元素的innerHTML為 expression
的計算結果。默認情況下,innerHTML中的內容會使用 $sanitize 服務進行凈化。為使用這個功能,確保 $sanitize
有效,例如,包含 ngSanitize
到你的模塊依賴中 (它不在 Angular核心中)。如果你知道值是安全的,你也可以忽略凈化。如要這樣做,需要通過 $sce.trustAsHtml來綁定一個明確可信的值。 請參見Strict Contextual Escaping (SCE)下的例子。
注意:如果 $sanitize
服務無效,并且綁定的值不是明確可信的,你將得到一個錯誤(而不會使用它)。
<ANY
ng-bind-html="">
...
</ANY>
參數(shù) | 類型 | 詳述 |
---|---|---|
ngBindHtml | expression | 可求值表達式。 |
試下這里: 在文本框輸入文本并查看問候信息的變化。
<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');});