angularJs中将字符串转换为HTML格式


  1. 首先定义一个filter:
.filter(
  'to_trusted', ['$sce', function ($sce) {
    return function (text) {
      return $sce.trustAsHtml(text);
    }
  }]
)

  2.html中使用filter:

<div ng-bind-html="string|to_trusted"> {{string}} </div>

  3.将字符串中的关键字找出来并标记为红色:

$scope.string = string.replace(keyword, "<span style=color:red>" + keyword + "</span>");

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM