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

前端之家收集整理的这篇文章主要介绍了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>

猜你在找的Angularjs相关文章