函数的差异($scope)和['$scope',函数($scope)]在AngularJS中

时间:2021-10-26 13:36:33

I am new to AngularJS. I try to find out whats the difference between these two controller definitions:

我对盎格鲁-阿吉斯人还不熟悉。我试图找出这两个控制器定义的区别:

app.controller('simpleController', ['$scope', function($scope) {
}]);

app.controller('simpleController', function($scope) {
});

I always use the second example, but sometimes I see people using the first example. Why should I do that? Is the controller in the first example inheriting another $scope variable?

我总是用第二个例子,但有时我看到人们用第一个例子。我为什么要这么做?第一个例子中的控制器是否继承了另一个$scope变量?

2 个解决方案

#1


1  

Those two controller definitions do the exact same thing. In the first definition, you're explicitly telling Angular the name of the dependency through the use of a string. This allows you to minify your code, since minifiers do not change the contents of strings.

这两个控制器的定义是完全相同的。在第一个定义中,通过使用字符串显式地告诉角化依赖项的名称。这允许您缩小代码,因为缩小器不会更改字符串的内容。

In the second definition, Angular infers what dependency to inject by looking at the parameter name, and thus minifying this code will break it.

在第二个定义中,通过查看参数名,角度推断要注入什么依赖项,从而缩小这段代码将破坏它。

#2


2  

The first example

第一个例子

app.controller('simpleController', ['$scope', function($scope) {
}]);

lets you minify your code

允许您简化代码

minifer converts $scope to variable a.but its identity is still preserved in the strings. so use first example if you would like to minify your code later.

minifer将$scope转换为变量a。但是它的性质仍然保留在字符串中。因此,如果以后想要缩小代码,请使用第一个示例。

#1


1  

Those two controller definitions do the exact same thing. In the first definition, you're explicitly telling Angular the name of the dependency through the use of a string. This allows you to minify your code, since minifiers do not change the contents of strings.

这两个控制器的定义是完全相同的。在第一个定义中,通过使用字符串显式地告诉角化依赖项的名称。这允许您缩小代码,因为缩小器不会更改字符串的内容。

In the second definition, Angular infers what dependency to inject by looking at the parameter name, and thus minifying this code will break it.

在第二个定义中,通过查看参数名,角度推断要注入什么依赖项,从而缩小这段代码将破坏它。

#2


2  

The first example

第一个例子

app.controller('simpleController', ['$scope', function($scope) {
}]);

lets you minify your code

允许您简化代码

minifer converts $scope to variable a.but its identity is still preserved in the strings. so use first example if you would like to minify your code later.

minifer将$scope转换为变量a。但是它的性质仍然保留在字符串中。因此,如果以后想要缩小代码,请使用第一个示例。