leaving directives aside, can , assign controllers somewhere in middle of view, modify particular piece html state or behavior. can on level, top level. view , controller tightly coupled together.
what reason can assign controller on route level well? never understood this, philosophy behind this? added benefit of assigning controller outside of view rather in top of view itself. surely not ability switch controller implementation; i've never seen done.
/edit: perhaps necessary in order manual resolve gets injected? (see john papa's example)
i'm sure there other valid reasons having option define controller route, think pretty big one. think of scenario:
- you have template you'd reuse various routes/states
- you may want assign different controller template based on route.
if use ng-controller
in template, can't (to knowledge) reuse template different controller.
with assigning controller via route, have more flexibility.
standard approach
i'm going use ui.router in example that's have experience with.
.state( 'users', { templateurl: 'person.html' controller: 'userscontroller' }) .state( 'admin', { templateurl: 'person.html' controller: 'admincontroller' })
same template, different routes, different controllers.
dynamic approach
surely not ability switch controller implementation
i won't argue merits/demerits of use-case i've used myself once or twice , i've seen discussed here on , other blogs.
.state( 'entity', { templateurl: 'person.html' controller: function( ...injectables ){ var usercontroller = function(){ ... } var admincontroller = function(){ ... } var controllerclass = isadmin ? admincontroller : usercontroller; return new controllerclass() } })
Comments
Post a Comment