AngularJS ng-repeat filter to apply if condition is met

583 views Asked by At

I have two arrays. One is items and the other is items2.

And if items2 is an empty array I only want to show the one item in items. How can apply a condition to the filter limitTo?

The limitTo should only be considered if a certain condition is met.

Thank you!

2

There are 2 answers

1
Fleeck On BEST ANSWER

Ternary expression will help you!

Example: limitTo : !items2.length ? 1 : n

0
Faly On

You can manage the condition in a function:

$scope.getItems() {
     return items2.length === 0 ? [items[0]] : items;
}

<div ng-repeat="item in getItems()"> {{ item.x }}</div>