Simplest possible Angular code? (ng-switch)

185 views
Skip to first unread message

Lee Whitney

unread,
Mar 3, 2013, 1:42:23 PM3/3/13
to ang...@googlegroups.com
To display an array of JSON objects, with varying types, do you think this is as simple as the code can be?


My regret is having to use a separate ng-switch directive for each type of object, which gets pretty verbose if you have lots of types.

I guess I could pre-process the JSON I get to make it more angular template friendly, but I'm not sure if adding another moving part in the code reduces overall complexity.



Peter Bacon Darwin

unread,
Mar 3, 2013, 2:09:32 PM3/3/13
to ang...@googlegroups.com
As you say you'd be better off refactoring the data for presentation but here is an interesting trick using a second ng-repeat directive.





--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Lee Whitney

unread,
Mar 3, 2013, 2:59:05 PM3/3/13
to ang...@googlegroups.com
Peter that's really slick, you're a stud :).

I'm experimenting with writing a Mongo style interactive query builder.  Basically parsing an expression tree with Angular templates so any simplicity is important.

Only thing a bit odd is the mismatched tags, but it's easily understandable.  And when code is easily understandable, less is always better.

Sander Elias

unread,
Mar 4, 2013, 3:04:59 AM3/4/13
to ang...@googlegroups.com
Hi Lee,

Is there an reason you can't use an ng-include in your repeat? I would create a small template for each type off object and then do something like:
<div ng-include="{{verhicle.type}}.html"> 
This is out off my head, and completely untested, but worth I try in my book ;)

regards
Sander Elias

Dave Merrill

unread,
Mar 4, 2013, 6:56:44 AM3/4/13
to ang...@googlegroups.com
+1
Reply all
Reply to author
Forward
0 new messages