为什么angularjs控制器声明有这样的句法结构? [英] Why do angularjs controller declaration have this syntax structure?

查看:134
本文介绍了为什么angularjs控制器声明有这样的句法结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到下面的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种注释


  1. 隐注释 - 第一个例子code

  2. 内联阵注释 - 第二个例子中code

  3. $注入性注解 - 在$注射方法

  4. $ 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:

  1. Implicit Annotation - the first example code
  2. Inline Array Annotation - the second example code
  3. $inject Property Annotation - the $inject method
  4. $ngInject Comment Annotation - the @ngInject method

这篇关于为什么angularjs控制器声明有这样的句法结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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