generator-cg-angular
generator-cg-angular copied to clipboard
[Question] Controller specified in view
Why controller is specified in view:
<div class="col-md-12" ng-controller="LoginCtrl">
</div>
instead of in route:
.when('/login', {
templateUrl: 'partail/login.html',
controller: 'LoginCtrl' })
?
Isn't it good practice to do it in route ?
I agree with your comment. The workflow I've created for myself is to specify the controller in the $stateProvider. I prefer to specify controller directly, in it's own 'controller/' directly, as we do with services.
Additionally, I prefer to use a 'view' file instead of partial. I'm waiting to get my separate controller command accepted, and then I have some view commands I'd like to submit as well.
There are two primary differences between the controller specified using ng-controller vs inside the route declaration:
- In route declaration, you can include the
resolve
property to have dependencies loaded and injected into the controller. - Using
ng-controller
, you can drop the partial into another template usingng-include
.
In my experience, ng-include
is used much more than resolve
and hence the choice to generate the partials using ng-controller