依赖注入角JS [英] Dependency injection in Angular JS

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

问题描述

我已经阅读了AngularJS文档,但仍然没有答案,我明白了。

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 function parameters.

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

推荐答案

如果你运行如下这个code:

解决方案
If you minify this code:

您将与(像)结束

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

角将无法注入的依赖,因为参数的名称都将丢失。

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

在另一方面,如果你再缩小这个code:

On the other hand, if you minify this code:

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

您将与结尾:

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

参数名称可:角的DI是可操作

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

这篇关于依赖注入角JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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