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

查看:25
本文介绍了Angular 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) {
  // ...
}]);

推荐答案

如果您缩小此代码:

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

你会以(类似的)结尾:

You'll end with (something like):

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

Angular 将无法注入依赖项,因为参数名称丢失.

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) {
  // ...
}]);

你会以:

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

参数名称可用:Angular 的 DI 是可操作的.

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

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

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