AngularJS - 依赖注入 [英] AngularJS - dependency injection

查看:33
本文介绍了AngularJS - 依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道下两行之间是否有区别,以及为什么要使用其中的一个(这两个按预期工作)

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 不知道 et 是什么.将其与缩小第二个版本进行比较:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆