Rodney Wormsbecher Rodney Wormsbecher - 16 days ago 5
AngularJS Question

Angular 1.5 compare ng-repeat $index with number

I'm trying to do something very simple in Angular but can't seem to get it working. I want to use an ng-repeat and compare the $index with a number. somehow there is an error in my code, but i cannot find it. Any help will be uch appreciated. Cheers!

<li ng-repeat="(key, structure) in structures">
{{ $index }}

<div class="row" ngIf="{{$index}} == 1">
<div class="col8 itemBox"></div>
<div class="col4-odd-fix itemBox"></div>
</div>
<input style="display: inline-block" name="structure" type="radio" value="{{key}}" ng-model="$parent.structure" required>
{{key}}
</li>

Answer

This should work:

<li ng-repeat="(key, structure) in structures">
    {{ $index }}
    <div class="row" ng-if="$index === 1">
        <div class="col8 itemBox"></div>
        <div class="col4-odd-fix itemBox"></div>
    </div>
    <input style="display: inline-block" name="structure" type="radio" value="{{key}}" ng-model="$parent.structure" required> {{key}}
</li>

You had a couple of issues:

  1. You need to use ng-if, ngIf is Angular 2 syntax.
  2. You shouldn't use {{ }} inside ng-if, you just need $index === 1.