我们可以在"过滤器"中使用$ sce.trustAsHtml(字符串)吗?

 良心無悔1314_878 发布于 2023-02-11 15:01

HTML:

this should be replaced

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('
{{item.n}}.{{item.label}}
'); //"case 2" var testElement = angular.element('
{{item.n}}
'); scope.testClass = attrs.class; scope.items = [ //{n:10,label:$sce.trustAsHtml('label 11')}, // "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:'label 22'}, {n:30,label:'label 33'} ]; 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:http: //jsfiddle.net/isnigirev/c2wRq/

问题:1)是否可以在过滤器上下文中使用trustAsHtml?2)如果不使用"ng-bind-html"指令,如何使用"how-to-remove-this-div"类除掉div?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有