Angular中自定义filter输出富文本内容

使用环境:

直接把后台输出的来自富文本编辑器的包含HTML标签的代码,原样输出。

  1. 设置相关Angular过滤器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    //app.js
    angular.module('xxx').filter(
    'to_trusted', ['$sce', function($sce) {
    return function(text) {
    return $sce.trustAsHtml(text); //直接把内容作为HTML使用
    }
    }]
    )
    .filter('no_html',function(){
    return function(input){
    return input.replace(/<[^>]+>/g,"");//去掉所有的html标记
    }
    });
  2. 编写相应的模板代码

    1
    2
    3
    <!-- template.html -->
    <div class="xxx" ng-bind-html="html_content | to_trusted"></div>
    <div class="xxx">{ {html_content2 | no_html}}</div>
文章目录
  1. 1. 使用环境:
,