Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么函数是角的';数组元素的DI内联注释?_Javascript_Arrays_Angularjs_Dependency Injection - Fatal编程技术网

Javascript 为什么函数是角的';数组元素的DI内联注释?

Javascript 为什么函数是角的';数组元素的DI内联注释?,javascript,arrays,angularjs,dependency-injection,Javascript,Arrays,Angularjs,Dependency Injection,我有个问题要问angularjs的人 所以,我使用angular已经有一段时间了。然而,每当我编写一个新的控制器或使用依赖注入的东西时,我发现自己误写了内联定义 someModule.controller('MyController', ['dep1', 'dep2', function (dep1, dep2) { ... }]); 我理解它是如何工作的,但是为什么那些有棱角的家伙们没有决定采用一种更普遍的方法呢?例如requirejs方式 someModule.controller('

我有个问题要问angularjs的人

所以,我使用angular已经有一段时间了。然而,每当我编写一个新的控制器或使用依赖注入的东西时,我发现自己误写了内联定义

someModule.controller('MyController', ['dep1', 'dep2', function (dep1, dep2) {
  ...
}]);
我理解它是如何工作的,但是为什么那些有棱角的家伙们没有决定采用一种更普遍的方法呢?例如requirejs方式

someModule.controller('MyController', ['dep1', 'dep2'], function(dep1, dep2) {
  ...
});
困扰我的是,第二个参数是一个依赖项数组和同时作为最后一个元素的回调。实际上,整个模块代码都写在最后一个数组元素中

将依赖项放在一个额外的数组中不是更好吗?通过这种方式,我们可以轻松地将依赖项数组动态传递到定义中

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

我觉得这很尴尬,但从未真正思考过背后的原因。有人能给我解释一下吗

我不知道这种语法背后的实际原因,但我认为它与一致性有关——无论您需要在何处注入服务,您都应该能够使用相同的语法

大多数地方使用示例中的语法:
module.controller
module.factory
等。在这些地方,语法可能类似于requirejs

但是,在定义指令时,还可以将服务注入其控制器。如果指令的控制器将被其他指令使用,例如
ngModel
指令,则通常会执行此操作

module.directive('myDirective', function () {
    return {
        controller: ['$scope', '$element', function ($scope, $element) {
            // ...
        }]
    };
});
module.controller('myDirectiveCtrl', ['$scope', '$element', function ($scope, $element) {
    // ...
}]);
module.directive('myDirective', function () {
    return {
        controller: 'myDirectiveCtrl'
    };
});
在这种情况下,您不能使用requirejs样式,但是数组样式可以工作。我想这可能是语法保持不变的原因之一。可能还有其他人

作为补充说明,您可以将该指令的控制器定义为普通控制器,但这会使代码更加冗长,此外,您还可以在该指令以外的其他位置使用该控制器

module.directive('myDirective', function () {
    return {
        controller: ['$scope', '$element', function ($scope, $element) {
            // ...
        }]
    };
});
module.controller('myDirectiveCtrl', ['$scope', '$element', function ($scope, $element) {
    // ...
}]);
module.directive('myDirective', function () {
    return {
        controller: 'myDirectiveCtrl'
    };
});
然后定义指令

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

您不必再对数组使用语法。您可以这样编写代码:

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