Angularjs NG绑定HTML的不安全更换 [英] Angularjs ng-bind-html-unsafe replacement

查看:145
本文介绍了Angularjs NG绑定HTML的不安全更换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前可以使用 NG-结合HTML的不安全来输出unsanitized code(因为消毒情况服务器端)。

I used to be able to use ng-bind-html-unsafe to output unsanitized code (because sanitization happens serverside).

但现在该选项不见了?我知道我可以使用 $ sce.trustAsHtml ,但补充说,对JavaScript所有的地方是一个巨大的痛苦时,不安全是很容易使用。

But now that option is gone? I know I can use $sce.trustAsHtml but adding that to the JavaScript all over the place is a huge pain when unsafe was so easy to use.

如何获得不安全了?

推荐答案

嗯,这很简单,只是创建自己的指令,下面是一个例子。

Well, it's quite simple to just create your own directive, here is an example.

指令

app.directive('bindHtmlUnsafe', function( $compile ) {
    return function( $scope, $element, $attrs ) {

        var compile = function( newHTML ) { // Create re-useable compile function
            newHTML = $compile(newHTML)($scope); // Compile html
            $element.html('').append(newHTML); // Clear and append it
        };

        var htmlName = $attrs.bindHtmlUnsafe; // Get the name of the variable 
                                              // Where the HTML is stored

        $scope.$watch(htmlName, function( newHTML ) { // Watch for changes to 
                                                      // the HTML
            if(!newHTML) return;
            compile(newHTML);   // Compile it
        });

    };
});

用法

<div bind-html-unsafe="testHTML"></div>

演示: http://jsfiddle.net/cC5VZ/2

Demo: http://jsfiddle.net/cC5VZ/2

这篇关于Angularjs NG绑定HTML的不安全更换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆