Difference in controller declaration in AngularJS

前端 未结 3 413
执念已碎
执念已碎 2021-01-21 06:01

I have seen controller being declared in two ways as below. But what diff does this make?

  1. appmodule.controller(\'Actrl\',[\'$scope\',function($scope) {}]);
3条回答
  •  走了就别回头了
    2021-01-21 06:34

    These are just two ways that AngularJS does Dependancy Injection. But this version,

    appmodule.controller('Actrl',['$scope',function($scope) {}]);
    

    in particular has been written to handle code minification. It is recommended use this version whenever possible.

    To get the difference clear, you must first understand how AngualarJS does dependancy injection. For more details you can refer to:

    1. Understanding Dependency Injection
    2. The "Magic" behind AngularJS Dependency Injection
    3. AngularJS Dependency Injection - Demystified

    But to cut the long story short, AngularJS loops through each items by their names in the parameter list, looks up against a list of known names of objects that can be injected and then injects the objects if there is a match.

    Let's have a look at an example:

    appmodule.controller('myController',function($scope, $log) {
        $log.info($scope);
    });
    

    Here, since $scope and $log (the order that you specify them in the parameter list doesn't matter here) are known objects to AngularJS, it injects them into myController. But if you were to do:

    appmodule.controller('myController',function(someVar) {
         // ...
    });
    

    AngularJS doesn't know about the parameter someVar and it throws a dependancy error.

    Now let's come back to your example. Let me modify your 2nd version a bit:

    appmodule.controller('Actrl',function($scope, $log) {
        $log.info($scope);
    });
    

    If we use a minifier, let's see how this piece of code gets minified. I am using an online minifier for this purpose . After minification, it becomes:

    appmodule.controller("Actrl",function(o,l){l.info(o)});
    

    This is because, minifiers usually shorten the variable names to smallest size to save space. Notice how our $scope got renamed to o and $log to l.

    Now, if we run this code, AngularJS doesn't know about o and l and it is going to cry about missing dependencies as we understood earlier.

    AngularJS deals with this problem using the 1st version of Dependency Injection in your example. If it was:

    appmodule.controller('Actrl',['$scope','$log', function($scope, $log) {
        $log.info($scope);
    }]);
    

    After minification it becomes:

    appmodule.controller('Actrl',['$scope','$log',function(o,l){l.info(o)}]);
    

    Here, even though $scope and $log parameters were renamed to o and l respectively, the minifier didn't touch the strings '$scope' and '$log' and their order in the array.

    Now when AngularJS injector sees this version using array, it substitutes each item in the parameter list in the function with the corresponding objects in the array (provided the objects are known to AngularJS).

    So in our example, even after minification, AngularJS knows that it needs to substitute o with $scope and l with $log. Thus the code runs without any Dependancy Injection errors.

    But one important thing is to note here is that, when we use this version the order of the items specified in the array and the parameter list of the function of really matters. That is, if you were to do:

    appmodule.controller('Actrl',['$scope','$log', function($log, $scope) {
        $log.info($scope);
    }]);
    

    , it is going to blow everything up!

提交回复
热议问题