gpt4 book ai didi

angularjs - 自定义指令中的 ng-repeat : Syntax Error: Token '$index' is unexpected

转载 作者:行者123 更新时间:2023-12-02 11:26:25 25 4
gpt4 key购买 nike

在 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 值作为插值更改为 @ 注入(inject):

scope: { foo: '=', myIndex: '@' },

然后使用:

<my-directive ng-repeat="foo in foos" foo="foo" my-index="{{$index}}"/>

关于angularjs - 自定义指令中的 ng-repeat : Syntax Error: Token '$index' is unexpected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22129582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com