为什么angularjs控制器声明有这样的句法结构? [英] Why do angularjs controller declaration have this syntax structure?
问题描述
我看到下面的angularjs控制器句法结构的所有时间。
angular.module('7minWorkout')。控制器('WorkoutController',
['$范围,$间隔,$位置',
功能($范围,$区间,$位置)
{
}]);
为什么在参数名称的重复?为什么不这样
angular.module('7minWorkout')。控制器('WorkoutController',
['$范围,$间隔,$位置',
功能()
{
}]);
或
angular.module('7minWorkout')。控制器('WorkoutController',
[
功能($范围,$区间,$位置)
{
}]);
array语法会帮助你缩小/丑化你的 JS的
code。
angular.module('7minWorkout')。控制器('WorkoutController',
功能($范围,$区间,$位置){
// code在这里
});
是精缩和错位为:
angular.module('7minWorkout')。控制器('WorkoutController',
功能(A,B,C){
// code在这里
});
所以角就不能找出哪些依赖注入
在另一方面,使用阵列
声明:
angular.module('7minWorkout')。控制器('WorkoutController',
['$范围,$间隔,$位置',函数($范围,$区间,$位置){
// code在这里
}]);
是精缩为:
angular.module('7minWorkout')。控制器('WorkoutController',
['$范围,$间隔,$位置'功能(A,B,C){
// code在这里
}]);
所以角就知道了 A
, B
和 C
重新present。
还有另一种方式,如果你使用你的第一个例子code像下面注入的变量:
WorkoutController $注射='$范围,$间隔,$位置'];
或
angular.module('7minWorkout')。控制器('WorkoutController',/ * * @ngInject /
功能($范围,$区间,$位置){
// code在这里
});
这将创造在code被标注上述 $注
方法。
那么,主要有4种注释:
- 隐注释 - 第一个例子code
- 内联阵注释 - 第二个例子中code
- $注入性注解 - 在$注射方法
- $ ngInject评论注释 - @ngInject 方法
I see the following angularjs controller syntax structure all the time.
angular.module('7minWorkout').controller('WorkoutController',
['$scope', '$interval', '$location',
function ($scope, $interval, $location)
{
}]);
Why the repetition in the parameter names? Why not just like this
angular.module('7minWorkout').controller('WorkoutController',
['$scope', '$interval', '$location',
function ()
{
}]);
or
angular.module('7minWorkout').controller('WorkoutController',
[
function ($scope, $interval, $location)
{
}]);
The array syntax will help you with minification/uglify of your js
code.
angular.module('7minWorkout').controller('WorkoutController',
function ($scope, $interval, $location) {
// code here
});
Will be minified and mangled as:
angular.module('7minWorkout').controller('WorkoutController',
function (a, b, c) {
// code here
});
So Angular won't be able to figure out which dependencies to inject
On the other hand, using the array
declaration:
angular.module('7minWorkout').controller('WorkoutController',
['$scope', '$interval', '$location', function ($scope, $interval, $location) {
// code here
}]);
Will be minified as :
angular.module('7minWorkout').controller('WorkoutController',
['$scope', '$interval', '$location', function (a, b, c) {
// code here
}]);
So Angular will know what a
, b
and c
represent.
There's also another way to inject variables if you use your first example code like below:
WorkoutController.$inject = ['$scope', '$interval', '$location'];
or
angular.module('7minWorkout').controller('WorkoutController', /* @ngInject */
function ($scope, $interval, $location) {
// code here
});
which will create the $inject
method mentioned above when the code is annotated.
So, there are mainly four kinds of annotation:
- Implicit Annotation - the first example code
- Inline Array Annotation - the second example code
- $inject Property Annotation - the $inject method
- $ngInject Comment Annotation - the @ngInject method
这篇关于为什么angularjs控制器声明有这样的句法结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!