本文介绍了Angularjs ng-bind-html-unsafe 替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我曾经能够使用 ng-bind-html-unsafe
来输出未净化的代码(因为净化发生在服务器端).
I used to be able to use ng-bind-html-unsafe
to output unsanitized code (because sanitization happens serverside).
但是现在那个选项不见了?我知道我可以使用 $sce.trustAsHtml
但是当 unsafe 很容易使用时,将它添加到 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
这篇关于Angularjs ng-bind-html-unsafe 替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!