angular filter 过滤内容高亮显示 依赖ngSanitize

1、 引入依赖文件

<script src="//cdn.bootcss.com/angular-sanitize/1.6.3/angular-sanitize.js"></script>

2、配置module

var app = angular.module('app', [
    'ngSanitize'
])

3、自定义过滤

.filter("color", function ($sce) {
        return function (text, search) {
            console.log(text);
            console.log(search);
            var html = text.replace(search, "<span style='color: red'>" + search + "</span>");
            var data = $sce.trustAsHtml(html);
            console.log(data);
            return data;
        };
    });

4、模板中使用 ng-bind-html

<p ng-bind-html="$ctrl.name | color: 'zxc'"></p>
    原文作者:zxc19890923
    原文地址: https://segmentfault.com/a/1190000008805229
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞