Ng-repeat in user directive: Syntax error: token '$ index' is unexpected

In AngularJS, this happens with an error:

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

Error message:

Error: [$parse:syntax] Syntax Error: Token '$index' is unexpected, expecting [:] at column 3 of the expression [{{$index}}] starting at [$index}}].

Here's the directive:

app.directive('myDirective', function() {
    return {
        restrict: 'E',
        scope: { foo: '=', myIndex: '=' },
        templateUrl: 'directives/myDirective.html'
    };
});

This is only a problem with custom directives. If I try this:

<div ng-repeat="foo in foos" style="padding: {{$index}}px;">
    index == {{$index}}
</div>
+4
source share
1 answer

Since you use =scope to declare your isolated property, Angular expects a property that is not interpolated:

To enter a value $indexwhen changing the interpolated value to @:

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

And then use:

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

Source: https://habr.com/ru/post/1529668/


All Articles