我有一个简单的字符串如下:
var templateString = "<span>This is a test</span>"
此字符串是一个指令的link
函数中定义。
现在,link
功能里面,我执行以下代码:
scope.$eval(templateString);
我的下一步是$compile
数据,并将其与范围相关联。
但是,我得到的错误,当我做$eval
:
Uncaught Error: Syntax Error: Token 'span' is an unexpected token at
column 2 of the expression [<span>This is a test</span>]
starting at [span>This is a Test</span>].
但是,如果我看位于here的文档,我似乎已经进行了正确但该字符串不评估的步骤。
编辑:我使用的是下面的示例文件:
angular.module('compile', [], function($compileProvider) {
// configure new 'compile' directive by passing a directive
// factory function. The factory function injects the '$compile'
$compileProvider.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);
}
);
};
})
});
我没有使用$watch
然而,因为我不需要看任何表情和我已经有了模板(templateString) 。
该文档似乎没有提及'$ eval'的任何地方。您应该直接'编译'模板并为其提供创建'element'的范围,除非您通过'attrs'元素获取该字符串,否则您不需要任何'$ eval'。 –
@musically_ut我更新了我的问题,以表明我正在使用的示例。如果我不评估,那么在span标签中的任何角度表达式都会被显示,因为它没有先评估它。我希望角度表达式(如果有的话)在我编译之前进行评估。 – callmekatootie