在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) 小于符号(<) ---> <
否则系统报错: