本文介绍了AngularJS - 依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有两下一行,以及为什么使用它其中的一个(如预期两项工作)之间的差异
I would like to know if there is a difference between the two next lines and why use it one of those (the two work as expected)
phonecatApp.controller('PhoneListCtrl', function($scope, $http) {...});
phonecatApp.controller('PhoneListCtrl', ['$scope', '$http', function($scope, $http) {...}]);
我把它从官方AngularJS教程,我知道有这个修改的解释,但我不明白它...
在此先感谢!
推荐答案
如果您压缩第一行你会得到:
If you minify your first line you get:
phonecatApp.controller("PhoneListCtrl",function(e,t){})
依赖注入将无法再工作,因为角度不知道是什么电子
和 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){}])
功能参数仍然改名,但 $范围
和 $ 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 - 依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!