Skip to content Skip to sidebar Skip to footer

Ng-if Not Comparing $index In Ng-repeat

In my code I want to print only first word from the array, and I am using ng-if condition. HTML:
<

Solution 1:

ngIf comes from angular verison 1.2.1 but you used 1.0.3 So your ng-if is not worked. If you upgrade the angular version to latest it works charmly.

Otherwise my suggestion to use ngShow directive

Use ng-show in this context and simply use $first for show first element in the array

 <div ng-show="$first" class="preview">{{Word}}- {{$index}}</div>

Fiddle

When we use ngIf and ngShow:

The ngIf directive removes or recreates a portion of the DOM tree based on an {expression}

The ngShow directive shows or hides the given HTML element based on the expression provided to the ngShow attribute

Solution 2:

An even better way would be to use $first instead of comparing $index

<divng-controller="MyCtrl"><divng-repeat="Word in Words"><divng-show="$first"class="preview">{{Word}}- {{$index}}</div></div></div>

Solution 3:

Use the latest version of AngularJS, it should work.

Solution 4:

Just use ng-show it wil work :)

<divng-controller="MyCtrl"><divng-repeat="Word in Words"><divng-show="$index == 0"class="preview">{{Word}}- {{$index}}</div></div></div>

Post a Comment for "Ng-if Not Comparing $index In Ng-repeat"