AngularJS - ngRepeat with several types of objects

I have a list of items. An element can be several things, say a list looks something like this:

[userObject , vehicleObject , userObject , animalObject , animalObject] 

Now I want to display a list with the ngRepeat directive, which will use the template according to the type of object (Polymorphic rendering). It can be done?

maybe something like ( ng-use ) is a hypothetical directive):

 <ul> <li ng-repeat="item in items"> <img ng-use="item.type == 'user'" ng-src="item.src"> <a ng-use="item.type == 'vehicle'">{{item.link}}</a> <span ng-use="item.type == 'animal'">{{item.name}}</span> </li> </ul> 
+23
javascript dom polymorphism angularjs rendering
Jan 20 '13 at 14:30
source share
2 answers
 <ul> <li ng-repeat="item in items" ng-switch="item.type"> <img ng-switch-when="user" ng-src="item.src"> <a ng-switch-when="vehicle">{{item.link}}</a> <span ng-switch-when="animal">{{item.name}}</span> </li> </ul> 

API Link:
http://docs.angularjs.org/api/ng.directive:ngSwitch

+31
Jan 20 '13 at 14:34
source share

Although this does not use ng-switch, it poses the problem of not having the type field that @DotNetHaggis pointed out.

 <div ng-repeat="field in fields"> <div ng-if="field.user !== undefined">user info</div> <div ng-if="field.vehicle !== undefined">vehicle info</div> <div ng-if="field.animal !== undefined">animal info</div> </div> 
0
Feb 23 '16 at 19:46
source share



All Articles