angularjs – 自定义指令中的ng-repeat:语法错误:令牌’$index’是意外的

前端之家收集整理的这篇文章主要介绍了angularjs – 自定义指令中的ng-repeat:语法错误:令牌’$index’是意外的前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在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}}"/>

猜你在找的Angularjs相关文章