generator-cg-angular icon indicating copy to clipboard operation
generator-cg-angular copied to clipboard

[Question] Controller specified in view

Open mgibas opened this issue 10 years ago • 2 comments

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 ?

mgibas avatar Feb 04 '15 04:02 mgibas

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.

robr24 avatar Feb 11 '15 12:02 robr24

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 using ng-include.

In my experience, ng-include is used much more than resolve and hence the choice to generate the partials using ng-controller

cgross avatar Feb 12 '15 16:02 cgross