使用ng-bind-html标签 注意事项

在AngularJS中,使用ng-bind-html可以显示包含HTML标签的文本,避免使用ng-bind。但需要注意,文本中的大于号(>)和小于号(<)需要转义为&gt;和&lt;,否则会导致系统错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       在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)  大于符号(>)   ---> &gt;

      (2)  小于符号(<)   ---> &lt;

否则系统报错:

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值