Personally when I get that far into dealing with controllers, it's time to
make a Directive. You could do a <user-widget ng-repeat="user in users"
user-type="{{user.type}}" ... and so on. That's one of the big uses for
custom directives - create an isolated scope for each member of an
ng-repeat in an idiomatic fashion.Then in the directive's linking function, switch on the type and attach the appropriate methods or objects to the scope. So if you have a class hierarchy with Subscriber and Admin as siblings from User, or Admin being a child of User which overrides methods or whatever, you can just put something like scope.actions = scope.type == 'admin' ? Admin : Subscriber kind of thing. There's still an if-else chain in your linking function, but it's not terrible and all the actual methods are declared elsewhere in a Factory that you can inject on the directive declaration. e On Mon, May 26, 2014 at 6:55 AM, gwk <[email protected]> wrote: > How about something like this? > http://plnkr.co/edit/OcDxuE74lDRGlMaeNGRI?p=preview The whole > angular.extend and recreating the locals is a bit inelegant, it does work. > Ideally, defining a directive with {controller: '=' } would work... but > that doesn't seem to (the ngController directive itself is just {scope: > true, priority: 100, controller: '@'}) > > > On Monday, May 26, 2014 3:04:28 AM UTC+2, Eric Marthinsen wrote: >> >> 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. > -- 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.
