I'm wondering is it possible to check what collection to use inside ng-repeat
?
For example, in my controller I have 2 arrays of data fetched from server, now I use ng-switch
to switch between them, check this jsbin - http://jsbin.com/diyefevi/1/edit?html,js,output
The problem is that these li
views in my real application are big but very similar.. so I really would like to use 1 ng-repeat
instead of 2.
So I wonder if something like ng-repeat="book in if list==='adultBooks' adultBooks else childBooks"
is possible in Angular?
Thanks!
The simplest solutions I can think of would be to define a new array on the scope which you set the other arrays to when you need.
E.g. http://jsbin.com/diyefevi/4/edit?html,js,output
Something like this would eliminate the need for ng-switch:
and the js:
Here is the jsbin code
Try this ...
In your controller
In your Html
MVVM Pattern advises to put our logic always in the controller and not in the view(HTML). If you ever find yourself adding "logic" in your view probably there is an alternate "better" way to do it.
But just for "fun" you can do this too:
Like this:
Here is the full sample:
http://jsbin.com/diyefevi/5/edit?html,js,output