AngularJS__绑定变量HTML文本的显示

时间:2021-01-03 19:38:29

今天在绑定变量中使用了HTML标签,但是标签一直都作为纯文本显示。

如:

<strong> bb </strong>


几番搜索以后,发现:

1. 要使用$sce, 需要导入var app=angular.module('myApp', ['ngSanitize']);

2. 需要引入angular-sanitize.js

3. HTML页面使用ng-bind-html,<p ng-bind-html="doc.chinese | to_trusted"></p>

4. 定义一个to_trusted的filter,如下:

app.filter('to_trusted', ['$sce', function($sce){
        return function(text) {
            return $sce.trustAsHtml(text);
        };
    }]);