在AngularJs中,当想要呈现的后台文本中有HTML标签的时候,使用ng-bind-html来代替ng-bind,就能正常的使用想用的HTML标签了。但要注意文本中如果使用到符号的话,注意要转义符号。
1>ng-bind-html的使用例子:
index.html
<div ng-controller="ExampleController">
<p ng-bind-html="myHTML"></p>
</div>
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');
});script.js
angular.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>';
}]);
结果: <a href="#">links!</a> and other <em>stuff</em> 被解析了
效果图:
但要注意文本中如果使用到符号的话,注意要转义符号
如: (1) 大于符号(>) ---> >
(2) 小于符号(<) ---> <
否则系统报错:
AngularJS ng-bind-html 使用详解及注意事项
在AngularJS中,使用ng-bind-html可以显示包含HTML标签的文本,避免使用ng-bind。但需要注意,文本中的大于号(>)和小于号(<)需要转义为>和<,否则会导致系统错误。
303

被折叠的 条评论
为什么被折叠?



