One can define controller for the state in ui router as controller: 'MainController', but also this can be omitted, and instead controller can be defined in view (template url) as ng-controller="MainController", what is the better practice?
1 Answer
The better practice is to define the controller in the provider configuration (this also works with the core $routeProvider btw). It's best to keep your view as 'clean' as possible.
As @JBNizet added, this is also the only way for the router to inject the resolved dependencies into the controller (see the resolve property that you can pass into your route/state config)
8 Comments
JB Nizet
But more importantly, that's the only way for the router to injected the
resolved dependencies to the controller.Robin-Hoodie
Correct, I'll add it to to my answer
Drumbeg
I have an Angular app in which my controller is defined by my routeProvider, however, in the associated view, I have references to scope member variables so the view still feels tightly coupled to my controller. Am I doing something wrong?
Robin-Hoodie
@Drumbeg Referencing to scope member variables isn't wrong no
Drumbeg
Cheers @NexusDuck. I realised a while back that my view is actually referencing a model, not a controller, which is more in line with how I picture MVC to work.
|