在AngularJS中,此操作失败并显示错误:
<my-directive ng-repeat="foo in foos" foo="foo" my-index="{{$index}}"/>
错误信息:
Error: [$parse:syntax] Syntax Error: Token '$index' is unexpected, expecting [:] at column 3 of the expression [{{$index}}] starting at [$index}}].
这是指令:
app.directive('myDirective', function() {
return {
restrict: 'E',
scope: { foo: '=', myIndex: '=' },
templateUrl: 'directives/myDirective.html'
};
});
这似乎只是自定义指令的问题。如果我尝试这样做:
<div ng-repeat="foo in foos" style="padding: {{$index}}px;">
index == {{$index}}
</div>
由于您使用的=
是声明隔离的范围属性,因此Angular希望使用不插值的属性:
为了插入$index
值,将内插值更改为@
:
scope: { foo: '=', myIndex: '@' },
然后使用:
<my-directive ng-repeat="foo in foos" foo="foo" my-index="{{$index}}"/>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句