Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way for long controller $inject #833

Closed
amiceli opened this issue May 17, 2017 · 7 comments
Closed

Best way for long controller $inject #833

amiceli opened this issue May 17, 2017 · 7 comments

Comments

@amiceli
Copy link

amiceli commented May 17, 2017

For example with this controller :

angular.module('lsi.controllers').controller('MainController', MainController);

MainController.$inject = [
    '$scope', '$q', 'Api', 'segments', 'ResourceService', 'AdminService','SweetAlertService', 'UsersCsvService', 'UsersSearchService', 'CriteriaService', '$rootScope'
];
    
function MainController($scope, $q, Api, segments, ResourceService, AdminService, SweetAlertService, UsersCsvService, UsersSearchService, CriteriaService, $rootScope) {
    // code here    
}

What's best way for too long $inject array and/or controller arguments ?

For controller is it a very bad idea to use arguments ?

function MainController() {
    // code here
    arguments[0].$on('', function() {});
}

Or use an alias :

function MainController($scp, $q, Api, segments, ResSce) {
    // code here
}

Thanks.

@MarcLoupias
Copy link

First, $scope, $rootScope and $q have nothing to do in your controller.

I use a max size line of 120 chars in my source code.

If the controller declaration goes away 120 chars, i fold line before the limit then i fold the $inject line regarding where i have folded the controller declaration.

Example :

MainController.$inject = [
    'Api', 'segments', 'ResourceService', 'AdminService','SweetAlertService', 'UsersCsvService',
    'UsersSearchService', 'CriteriaService'
];

function MainController(Api, segments, ResourceService, AdminService, SweetAlertService, UsersCsvService,
                        UsersSearchService, CriteriaService) {
    // code here
}

In this example with a 120 chars line, i could have this :

MainController.$inject = [
    'Api', 'segments', 'ResourceService', 'AdminService','SweetAlertService', 'UsersCsvService', 'UsersSearchService', 
    'CriteriaService'
];

function MainController(Api, segments, ResourceService, AdminService, SweetAlertService, UsersCsvService, 
                        UsersSearchService, CriteriaService) {
    // code here
}

But to keep consistency between $inject and controller declaration i fold line in $inject where i fold line in controller declaration.

I am not sure your arguments use is working with AngularJs injection system and minification.

@amiceli
Copy link
Author

amiceli commented May 17, 2017

Thanks for your answer.
120 chars limit is a good solution, I adopt it !

Why $rootScope and $scope should not be in my controller ?
How do you sent events without $on and $broadcast ?

@MarcLoupias
Copy link

For $rootScope and $scope you should read Y031.

IMHO there is no reason to inject them except when a third party library that you must use needs them to communicate with events. You have to keep in mind that scopes does not exist anymore in Angular. So if you want to handle your technical debt you need to keep consistency in your projects between AngularJS and Angular to allow a future migration when AngularJS will become deprecated.

For events, you should read this discussion.

@amiceli
Copy link
Author

amiceli commented May 17, 2017

Ok, I note it.
Thanks.

@amiceli amiceli closed this as completed May 17, 2017
@bampakoa
Copy link

@amiceli A nice alternative is ng-annotate which eliminates the need for lengthy $inject arrays:

/* @ngInject */
function MainController($scope, $q, Api, segments, ResourceService, AdminService, SweetAlertService, UsersCsvService, UsersSearchService, CriteriaService, $rootScope) {
    // code here    
}

@amiceli
Copy link
Author

amiceli commented May 17, 2017

@bampakoa thanks

@MarcLoupias An another question, without $scope, how to watch a property change event ? Like $watch

@amiceli amiceli reopened this May 17, 2017
@MarcLoupias
Copy link

Read this discussion. IMHO, $watch should be avoided as possible. Use ngChange instead.

@amiceli amiceli closed this as completed May 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants