Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-repeat on non array object

I know that ng-repeat works only on array.

My problem is when I don't know if the object that I get from the server, is array, or only object.
I need to determine dynamically if it's array or object, and only if it's array use ng-repeat.

My question is what is the best way implement using ng-repeat with condition - only if the object is an array?

I tried to solve the problem this way:

 <div ng-if="Array.isArray(myObj)"ng-repeat="item in myObj">
      <do-some-stuff item='item'></do-some-stuff>
 </div>
 <do-some-stuff ng-if="!Array.isArray(myObj)" item='myObj'></do-some-stuff>

But it's not working.

like image 584
cheziHoyzer Avatar asked Mar 19 '23 02:03

cheziHoyzer


1 Answers

ng-repeat priority is 1000 and ng-if priority is 600. so ng-repeat execute first and then ng-if.that's why your code not working .use this code:

<div ng-if="Array.isArray(myObj)">
      <div ng-repeat="item in myObj">
          <do-some-stuff item='item'></do-some-stuff>
      </div>
 </div>
 <do-some-stuff ng-if="!Array.isArray(myObj)" item='myObj'></do-some-stuff>
like image 91
Mukund Kumar Avatar answered Mar 20 '23 15:03

Mukund Kumar