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