Dependency injection in Angular JS

后端 未结 2 750
梦谈多话
梦谈多话 2020-12-20 05:45

I already read the AngularJS documentation but still don\'t have an answer which I understand.

Why is this used twice? One time as array elements, the second as func

2条回答
  •  死守一世寂寞
    2020-12-20 06:43

    If you minify this code:

    someModule.controller('MyController', function($scope, greeter) {
      // ...
    });
    

    You'll end with (something like):

    someModule.controller('MyController', function(a, b) {
      // ...
    });
    

    Angular won't be able to inject the dependencies since the parameters names are lost.

    On the other hand, if you minify this code:

    someModule.controller('MyController', ['$scope', 'greeter', function($scope, greeter) {
      // ...
    }]);
    

    You'll end with:

    someModule.controller('MyController', ['$scope', 'greeter', function(a, b) {
      // ...
    }]);
    

    The parameters names are available: Angular's DI is operational.

提交回复
热议问题