In AngularJS, this fails 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 only appears to be an issue with custom directives. If I try this:
<div ng-repeat="foo in foos" style="padding: {{$index}}px;">
index == {{$index}}
</div>
Since you are using =
to declare your isolated scope property, Angular is expecting a property that is not interpolated:
In order to inject the $index
value as the interpolated value change to @
:
scope: { foo: '=', myIndex: '@' },
And then use:
<my-directive ng-repeat="foo in foos" foo="foo" my-index="{{$index}}"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With