问题描述
HTML
<div ng-app="app">
<test-d class="my-class">this should be replaced</test-d>
</div>
JS:
angular.module('app', []).directive('testD', ['$compile','$sce', function($compile, $sce) {
return {
restrict: 'E',
link: function(scope, element, attrs, controller) {
//"case 1"
//var testElement = angular.element('<div class="{{testClass}}"><div ng-repeat="item in items">{{item.n}}.{{item.label}}</div></div>');
//"case 2"
var testElement = angular.element('<div class="{{testClass}}"><div ng-repeat="item in items">{{item.n}}<div class="how-to-remove-this-div" ng-bind-html="item.label|htmlize"></div></div></div>');
scope.testClass = attrs.class;
scope.items = [
//{n:10,label:$sce.trustAsHtml('<span class="with-icon">label 11</span>')},
// "case 1" of "testElement": no any effects (with "case 1" that would be preferred)
// "case 2" of "testElement": Error: [$sce:itype] Attempted to trust a non-string value in a content requiring a string: Context: html
{n:20,label:'<span class="with-icon">label 22</span>'},
{n:30,label:'<span class="with-icon">label 33</span>'}
];
var testElementCompiled = $compile(testElement)(scope);
//"case 3"
element.replaceWith(testElementCompiled);
//"case 4"
//element.replaceWith($sce.trustAsHtml(testElementCompiled));
}
}
}]).filter('htmlize', ['$sce', function($sce){
return function(val) {
return $sce.trustAsHtml(val);
};
}]);
的jsfiddle:
问题:
1)是有可能使用trustAsHtml出滤波器上下文的?
2)如果没有如何获得使用NG绑定HTML的指令的情况下,以如何做 - 删除 - 这-DIV阶级摆脱的div?
Questions: 1) is it possible to use trustAsHtml out of filter context ?2) IF NOT how to get rid of div with "how-to-remove-this-div" class in case of using "ng-bind-html" directive ?
推荐答案
得到它(trustAsHtml)的指令这里。在错误:[$ SCE:ITYPE]尝试在需要字符串内容信任一个非字符串值:上下文背景:HTML
时,我错误地尝试在发生错误相同的标签上呼吁trustAsHtml两次。
Got it (trustAsHtml) working in directive here. The "Error: [$sce:itype] Attempted to trust a non-string value in a content requiring a string: Context: html"
error was occured when i by mistake try to call trustAsHtml twice on the same "label".
问题的第二部分仍然存在,但我会用一些变通办法解决它。不管怎样,这很有趣,以解决它。
Second part of question still remains, but i will solve it with some workarounds. And anyway, it's interesting to resolve it too.
这篇关于我们可以用$ sce.trustAsHtml(串)出&QUOT;滤波器QUOT; S?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!