通过Angularjs 1.2编译从数据库加载的HTML
我正在将数据库中的内容直接加载到视图中,我希望对其进行编译,以便angular代码能够像通过标准模板加载一样执行 我不太明白的是如何编译从数据库加载的HTML。当我尝试使用$compile时,它总是会出错并出现indexOf问题通过Angularjs 1.2编译从数据库加载的HTML,angularjs,Angularjs,我正在将数据库中的内容直接加载到视图中,我希望对其进行编译,以便angular代码能够像通过标准模板加载一样执行 我不太明白的是如何编译从数据库加载的HTML。当我尝试使用$compile时,它总是会出错并出现indexOf问题 Error: [$parse:lexerr] Lexer Error: Unexpected next character at columns 1415-1415 [#] in expression [<p>Introducing XXX featur
Error: [$parse:lexerr] Lexer Error: Unexpected next character at columns 1415-1415 [#]
in expression [<p>Introducing XXX featuring Something great. Along with the already famous World <strong>Approach</strong>….
错误:[$parse:lexerr]Lexer错误:在第1415-1415列[#]出现意外的下一个字符
在表达中[介绍XXX的特色是一些很棒的东西。连同已经著名的世界方法…。
从其他评论中我可以看到,$compile似乎只适用于现有的DOM元素,但在我的例子中,新的SCE东西让我更加努力地将html放入视图中,而不剥离角度代码。我唯一能想到的是,每次内容更改时,可能都会使用手表进行编译。这是不是如何通过指令来完成
这个plunker展示了SCE以及如何在不剥离角度代码的情况下将HTML放入视图中(例如ng click)。
请看第二个例子,它使用了sce.trustAsHtml()
。
我如何扩展它,然后编译代码,使ng单击
工作
提前感谢。事实证明,如果我编译内容本身,我不需要担心SCE。因此,我不使用ng bind html,而是使用$compile(.$compile)中的确切指令将html放在适当的位置,同时编译它 要显示已编译的内容,请执行以下操作:
而不是
.directive('compile', function($compile) {
// directive factory creates a link function
return function(scope, element, attrs) {
scope.$watch(
function(scope) {
// watch the 'compile' expression for changes
return scope.$eval(attrs.compile);
},
function(value) {
// when the 'compile' expression changes
// assign it into the current DOM
element.html(value);
// compile the new DOM and link it to the current
// scope.
// NOTE: we only compile .childNodes so that
// we don't get into infinite loop compiling ourselves
$compile(element.contents())(scope);
}
);
};
});