Hi. I've got an app where I have a list of users. The users come in two 
varieties: subscribers and admins (more types are on their way). The 
different user types have different behaviors associated with them. I want 
to list of all the users and I want to associate them with the correct 
behaviors. Here's one implementation that works:

http://plnkr.co/edit/lHyLxAuF7GYOvN6n4ouc?p=preview

The core of that example is a UserCtrl and a bunch of conditionals to 
determine how to handle each user, based upon its type.

I'm not crazy about that implementation though. As the number of user types 
increases, each conditional needs to be revisited. I feel like it's a 
violation of the open-close principle and, overall, it's cumbersome.

Here's an implementation that I prefer. The only caveat is that it doesn't 
work.

http://plnkr.co/edit/PIrRHfD4tiTHyKpUvdZX?p=preview

The essence of this solution is that we have a SubscriberCtrl and AdminCtrl 
that we dynamically assign. What I really like about this solution is that 
it does away with all the conditionals. The behavior for each user type is 
encapsulated in a dedicated controller. Accommodating more user types only 
involves adding the user type to the switch statement in MainCtrl. At no 
point, do existing controllers need to be touched when new user types are 
added or when the behavior of a specific user type is modified.

I've heard some people say that using one controller per item in ngRepeat 
isn't idiomatic, but it feels right. My question is, what is the best way 
to accomplish what I'm trying to do? Naturally, it needs to work, but it 
should also be idiomatic.

One finer point about the examples is that the structure of the HTML is 
very deliberately chosen. It mirrors the setup in my app and, because of 
the necessity of the table, greatly reduces the possibility of introducing 
arbitrary elements into the DOM.

Cheers,
Eric

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to