Naguib Ihab
Naguib Ihab

Reputation: 4506

ng-repeat on one of two arrays based on condition

Say I have

<div ng-if="groupA.length > 0" ng-repeat="element in groupA"> .... </div>
<div ng-if="groupA.length == 0" ng-repeat="element in groupB"> .... </div>

Both divs are exactly the same HTML except for the if conditions and the fact that they repeat on two different groups. Am I able to join them together in one div element? I.e. <div ng-repeat="element in (groupA || groupB)"> ... </div>

I know I can check in the controller to and have a condition there like groupC = groupA.length > 0 ? groupA : groupB and have in the html <div ng-repeat="element in groupC> ... </div> but I was wondering if there's a way to do it on HTML directly?

Upvotes: 1

Views: 873

Answers (2)

Manoj Patidar
Manoj Patidar

Reputation: 1171

you can just add a condition inside the ng-repeat like as

<div ng-repeat="element in (groupA.length > 0 ? groupA : groupB)"> </div>

Hope this will help you

Upvotes: 1

Ankit Vadi
Ankit Vadi

Reputation: 475

Should work with ternary operators. Demo: https://plnkr.co/edit/yqrUb2UNAasFs4EKaf0b

<div ng-repeat="element in (groupA.length > 0 ? groupA : groupB)"> .... </div>

Upvotes: 2

Related Questions