Search code examples
javascripthtmlangularjsangularjs-directiveangularjs-ng-repeat

Access current item in ng-repeat


I have something like this:

<li ng-repeat="item in items">
    {{item.name}}
</li>

The item object has a property called index. How do I assign it to the tabIndex so my output looks like:

<li tabIndex="100">Mike</li>
<li tabIndex="101">Smith</li>

I've tried this but it cannot access the current item:

<li ng-repeat="item in items" tabIndex="item.index">
    {{item.name}}
</li>

Solution

  • <li ng-repeat="item in items" tabIndex="{{item.index}}">
        {{item.name}}
    </li>
    

    Note: If tabIndex happens to be a directive in your app with isolate scope, and you are using the @attr to get the value, the interpolation (braces) is needed. An alternative would be to leave them out and define the variable in your directive like =attr instead.