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>

张旭超
1.4k 声望222 粉丝

精通 html+div+css jquery, vue, angularjs, angular2, angular4, ionic, ionic2


引用和评论

0 条评论