AngularJS $index inside ng-switch

1.9k Views Asked by At

I am trying to access $index of ng-repeat and place it inside ng-switch-when. Here is the last thing I have tried. Small piece of what I am trying to do:

<li ng-repeat="contact in contacts" ng-switch on="MenuSelected">
 <span ng-switch-when="$index">{{ contact }} {{$index}}</span>
</li>

MenuSelected I have define in my controller with 0 for example, and I have 2 contacts for test. I have tried to mix ng things in different tags and everything fall (just show lines), so I don't think that is the problem. When I remove $index and place for example 0 everything show up.

1

There are 1 best solutions below

1
On BEST ANSWER

you cannot use expressions with ngSwitch

refrer this doc

Be aware that the attribute values to match against cannot be expressions. They are interpreted as literal string values to match against. For example, ng-switch-when="someVal" will match against the string "someVal" not against the value of the expression $scope.someVal

this implies that ng-switch-when="$index" $index is treated as a string not as a index value


use ngIf instead

    <span ng-if="$index == MenuSelected">{{ contact }} {{$index}}</span>

here is the working Fiddle