使用正常功能或数组符号控制器 [英] controllers using as normal function or array notation
问题描述
什么是这两个之间的区别:
What is the difference between these 2:
angular.module('myapp' ,[])
.controller('MyController', function($scope){...});
和
angular.module('myapp' ,[])
.controller('MyController, ['$scope', function($scope){...})];
这是为那些谁给新象AngularJS我相当复杂。语法是从Java和C太不一样了。
This is quite complicated for those who new to AngularJS like me. The syntax is too different from Java and C.
非常感谢。
推荐答案
有它们之间没有什么区别。无论code的工作方式相同。但是,如果你先用code,当你再缩小code,那么它会混淆。
There's nothing difference between them. Both code works same way. But if you use first code and when you minify the code then it will confuse.
寻找一个例子:
.controller('MyController', function(a){...});//$scope is changed to a
和您的code不会因为angularjs code工作使用$范围变量,它没有考虑第一,第二,第三,等参数。
And your code won't work as angularjs code uses $scope variable as it doesn't take first, second, third, and so on parameters.
所以,第二code是比第一,如果当你再缩小code,它仍然需要相同的变量,即$范围更加安全。
So, the second code is safer than first as if when you minify the code, it still takes same variable i.e. $scope.
寻找一个例子:
.controller('MyController', ['$scope', function(a){...})];//a refers to $scope
所以,当你再缩小code $为范围代替的注入上述code正常工作。所以,如果你将多个参数传递订货,然后在这个例子中的问题。请看下面:
So, the above code works fine when you minify the code as $scope is injected in place of a. So, if you pass multiple parameters then ordering matters in this example. Look at the following:
.controller('myController的',['$范围,$超时',功能(S,T){...});
s是作为注入范围$和T注入为$超时。所以,如果你改变它们的顺序如 ['$超时','$范围,功能(S,T){...})]
则S是$超时t为$范围。所以,在这个例子中,但你的第一个例子code订货订货事宜都不会有问题如姓名事宜像$范围,$超时。
.controller('MyController', ['$scope','$timeout', function(s,t){...})];
s is injected as $scope and t is injected as $timeout. So if you change the order of them like ['$timeout','$scope', function(s,t){...})]
then s is $timeout and t is $scope. So, ordering matters in this example but in your first example code ordering won't matter as name matters like $scope, $timeout.
还有另一种方式,如果你使用你的第一个例子code像下面注入的变量:
There's also another way to inject variables if you use your first example code like below:
MyController.$inject = ['$scope'];
有关多个参数,
MyController.$inject = ['$scope','$timeout'];
因此,有主要有3种注释:
- 隐注释 - 您的第一个例子code
- $注入性注解 - 在$注射方法
- 内联阵注释 - 你的第二个例子code
这篇关于使用正常功能或数组符号控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!