Search code examples
angularjsangular-ng-if

ngIf for iterating through two different Arrays


I have this piece of code:

<tr ng-repeat="row in someController.searchResults" 
    ng-class="{'selected': tableRow.selected}" 
    ng-click="selectRow(tableRow)" >

In this piece of code I'm iterating through searchResults array.

I need to add a condition (want to do it with ngIf).

  • If the condition is true I'll iterate through someController.searchResults
  • If condition is false I want to iterate through another Array e.g: someController.someOtherSearchResults

How can I achieve that?


Solution

  • You can use plain javascript in the ng-repeat attribute, like so:

    <tr ng-repeat="row in (someController.importantBoolean ? someController.searchResults : someController.someOtherSearchResults)" 
            ng-class="{'selected': row.selected}" 
            ng-click="selectRow(row)" >
    </tr>
    

    See the following plunker for an example: https://plnkr.co/edit/Nxh9gVB9GDP5FaLVDGrS