为什么在角的DI内嵌注解功能的数组元素? [英] Why is the function in angular's DI inline annotation a array element?

查看:106
本文介绍了为什么在角的DI内嵌注解功能的数组元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这里的乡亲angularjs的问题。

I have a question for the angularjs folks here.

所以,我采用了棱角分明相当长一段时间了。不过,每次当我写一个新的控制器或者一些正在使用依赖注入的时候,我发现自己的miswriting内联定义。

So, I am using angular for quite a while now. However, every time when I am writing a new Controller or something that is using dependency injection, I find myself miswriting the the inline definition.

someModule.controller('MyController', ['dep1', 'dep2', function (dep1, dep2) {
  ...
}]);

我理解它是如何工作的,但为什么角的家伙不能决定一个更常用的方法?例如,requirejs方式

I understand how it works, but why did the angular guys not decide for a more common approach? For example the requirejs way

someModule.controller('MyController', ['dep1', 'dep2'], function(dep1, dep2) {
  ...
});

是什么在困扰我,就是第二个参数是依赖的阵列和回调为在同一时间的最后一个元素。实际上,整个模块code被写入在最后一个数组元素。

What is bothering me, is that the second argument is a array of dependencies and the callback as the last element at the same time. In fact, the whole module code is written in the last array element.

那岂不是更好有一个额外的数组的依赖?通过这种方式,我们可以很容易地通过依赖关系的动态数组到一个定义。

Wouldn't it be better to have the dependencies in an extra array? This way we could easily pass a array of dependencies dynamically into a definition.

我觉得这个比较尴尬,但从来没有真正想过背后的原因。有人可以解释这样对我?

I find this rather awkward but never really thought about the reason behind. Can someone explain this to me?

推荐答案

我不知道这个语法背后的实际原因,但我认为它具有一致性做 - 您应该能够使用相同的语法,无论在那里你需要注入的服务。

I don't know the actual reason behind this syntax, but I assume it has to do with consistency --you should be able to use the same syntax regardless of where you need to inject services.

大多数地方使用的语法在您的示例: module.controller module.factory 等。在这些地方语法可能像蜂requirejs。

Most places use the syntax in your example: module.controller, module.factory etc. In those places the syntax could bee like requirejs.

然而,定义一个指令,也可以注入服务整合到它的控制器时。这通常是如果该指令的控制器将通过其他指令一起使用,例如在 ngModel 指令。

However, when defining a directive you can also inject services into its controller. This is usually done if the directive's controller will be used by other directives, e.g. the ngModel directive.

module.directive('myDirective', function () {
    return {
        controller: ['$scope', '$element', function ($scope, $element) {
            // ...
        }]
    };
});

在这种情况下,你不能使用requirejs风格,但数组风格的作品。我想这可能是原因语法是因为它是其中之一。有可能是别人。

In this case you can't use the requirejs style, but the array style works. I guess this could be one of the reasons the syntax is as it is. There might be others.

作为一个方面说明,你可以定义指令的控制器作为一个正常的控制器,但是这使得code更详细的,再加上你可能会用在其他地方比该指令的控制器。

As a side note you could define the directive's controller as a normal controller, but this makes the code more verbose, plus you could potentially use the controller in other places than the directive.

module.controller('myDirectiveCtrl', ['$scope', '$element', function ($scope, $element) {
    // ...
}]);

然后定义指令。

module.directive('myDirective', function () {
    return {
        controller: 'myDirectiveCtrl'
    };
});

这篇关于为什么在角的DI内嵌注解功能的数组元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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