AngularJS - 依赖注入 [英] AngularJS - dependency injection
问题描述
我想知道下两行之间是否有区别,以及为什么要使用其中的一个(这两个按预期工作)
I would like to know if there is a difference between the two next lines and why to use one of those (the two work as expected)
phonecatApp.controller('PhoneListCtrl', function($scope, $http) {...});
phonecatApp.controller('PhoneListCtrl', ['$scope', '$http', function($scope, $http) {...}]);
我从官方的 AngularJS 教程中获取了它,我知道有关于此修改的解释,但我不明白...http://docs.angularjs.org/tutorial/step_05
I took it from the official AngularJS tutorial and I know there is an explanation about this modification but I don't understand it... http://docs.angularjs.org/tutorial/step_05
提前致谢!
推荐答案
如果你缩小第一行,你会得到:
If you minify your first line you get:
phonecatApp.controller("PhoneListCtrl",function(e,t){})
依赖注入将不起作用,因为 Angular 不知道 e
和 t
是什么.将其与缩小第二个版本进行比较:
The dependency injection won't work then, because Angular has no idea what e
and t
are. Compare that to minifying the second version:
phonecatApp.controller("PhoneListCtrl",["$scope","$http",function(e,t){}])
函数参数仍然被重命名,但 $scope
和 $http
在数组中给出,因此注入可以按预期进行.
The function parameters are still renamed, but $scope
and $http
are given in the array so the injection can go ahead as expected.
这篇关于AngularJS - 依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!