Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS -> UI router, where to define controller

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?

like image 346
ClassyPimp Avatar asked Sep 07 '26 16:09

ClassyPimp


1 Answers

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)

like image 101
Robin-Hoodie Avatar answered Sep 10 '26 06:09

Robin-Hoodie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!