©
Dieses Dokument verwendetPHP-Handbuch für chinesische WebsitesFreigeben
创建一个绑定,使用安全的方式使当前元素的innerHTML为expression
的计算结果。默认情况下,innerHTML中的内容会使用 $sanitize 服务进行净化。为使用这个功能,确保$sanitize
有效,例如,包含ngSanitize
到你的模块依赖中 (它不在 Angular核心中)。如果你知道值是安全的,你也可以忽略净化。如要这样做,需要通过 $sce.trustAsHtml来绑定一个明确可信的值。 请参见Strict Contextual Escaping (SCE)下的例子。
注意:如果$sanitize
服务无效,并且绑定的值不是明确可信的,你将得到一个错误(而不会使用它)。
ng-bind-html="">...
参数 | 类型 | 详述 |
---|---|---|
ngBindHtml | expression | 可求值表达式。 |
试下这里: 在文本框输入文本并查看问候信息的变化。
ng-controller="ExampleController">ng-bind-html="myHTML">
script.js
angular.module('bindHtmlExample',['ngSanitize']).controller('ExampleController',['$scope',Function($scope){$scope.myHTML='I am an HTML
string with '+'links! and other stuff';}]);
protractor.js
it('should check ng-bind-html',Function(){expect(element(by.binding('myHTML')).getText()).toBe('I am an HTMLstring with links! and other stuff');});