ng-bind-html-unsafe的替代


angular 1.2以后(或更早?)移除了ng-bind-html-unsafe,那么我要用这个directive来绑定html代码怎么办?随便一测试,它是不支持把html直接传给它的:

    //html
   <p ng-bind-html="m"></p>

   //js
   $scope.m="<b>text</b>";

   //error:
   [$sce:unsafe] Attempting to use an unsafe value in a safe context.

参考这篇文章,得到解决。我选择的是直接做一个过滤器:

    //js
    app.filter('to_trusted', ['$sce', function ($sce) {
            return function (text) {
                return $sce.trustAsHtml(text);
            };
        }]);
    app.controller...{
        $scope.m="<b>text</b>"
    }   
    //html:
    <p ng-bind-html="m | to_trusted"></p>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM