首页 > Web开发 > 详细

ng-bind-html-unsafe的替代

时间:2014-02-20 15:55:27      阅读:658      评论:0      收藏:0      [点我收藏+]

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>

ng-bind-html-unsafe的替代

原文:http://www.cnblogs.com/walkerwang/p/3556847.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!