简体   繁体   English

Visual Studio的AngularJS模板解释

[英]Visual Studio's AngularJS Template Explained

When I create a new controller using VS2015 templates I get this code: 当我使用VS2015模板创建一个新控制器时,我得到以下代码:

(function () {
    'use strict';

    angular
        .module('app')
        .controller('controller', controller);

    controller.$inject = ['$scope']; 

    function controller($scope) {
        $scope.title = 'controller';

        activate();

        function activate() { }
    }
})();

Questions: 问题:

  1. Why does VS template wraps the code in a self invocing function? 为什么VS模板将代码包装在自调用函数中?

  2. What's the thing with the activate() function? activate()函数有什么用? What code do I suppose to write inside and why do I need a seperate function instead of just writing the code inside the controller? 我想在里面编写什么代码,为什么我需要一个单独的函数而不是只是在控制器中编写代码?

  3. Do the controller.$inject = ['$scope']; controller.$inject = ['$scope']; considered a better practice then writing the dependencies in an array (as an argument of the controller function). 被认为是一种更好的做法,然后在数组中编写依赖项(作为控制器函数的参数)。

Why does VS template wraps the code in a self invoking function? 为什么VS模板将代码包装在自调用函数中?

Because it prevents corrupting global namespace. 因为它可以防止破坏全局命名空间。 In this case, controller will become a member of global namespace in the absence of IIFE. 在这种情况下,在没有IIFE的情况下, controller将成为全局命名空间的成员。

What's the thing with the activate() function? activate()函数有什么用?

It helps separating declaration and execution of controller variables and code respectively. 它有助于分别分离控制器变量和代码的声明和执行。 Prior to calling the activate function, we usually declare all the dependencies as the controller members using this. 在调用activate函数之前,我们通常使用this.来声明所有依赖项作为控制器成员this. syntax. 句法。

You will write the executing code inside the activate function. 您将在activate函数中编写执行代码。

Do the controller.$inject = ['$scope']; controller.$inject = ['$scope']; considered a better practice? 被认为是更好的做法?

Actually yes! 其实,是! It helps you to separate your controller definition from it's angular counterpart. 它可以帮助您将控制器定义与其角度对应物分开。 It helps you to avoid writing the entire controller code in angular.module( ... ).controller block to improve readability of your code. 它可以帮助您避免在angular.module( ... ).controller . angular.module( ... ).controller块中编写整个控制器代码,以提高代码的可读性。

Edit 1 : 编辑1

Without the IIFE, the function named controller will become a member of the global namespace, and will be accessible throughout your page. 如果没有IIFE,名为controller的函数将成为全局命名空间的成员,并且可以在整个页面中访问。 It will also pose a possiblity of being overwritten by subsequent code. 它还有可能被后续代码覆盖。 Don't mix AngularJS with this, because this is something which JavaScript does. 不要将AngularJS与此混合,因为这是JavaScript所做的事情。

The activate function is just a layer to separate the concerns. activate功能只是一个分离问题的层。 You can write your code entirely in the function controller . 您可以在函数controller完全编写代码。 But in that way, it becomes harder to separate which code controller is executing, and which code binds the variables to controller. 但是以这种方式,分离哪个代码控制器正在执行以及哪些代码将变量绑定到控制器变得更加困难。 Take this example: 举个例子:

function controller($scope) {
  var vm = this;
  vm.data = [];
  $scope.title = 'controller';

  activate();

  ///
  function activate() {
    getData()
      .then(data => {
        // do something with data
      });

    // ...
  }

  function getData() {
    ...
  }
}

By reading the above code, one can easily know about the variables being used down the line in the controller, without specifically going into the depth of the controller. 通过阅读上面的代码,人们可以很容易地了解在控制器中使用的线路下面的变量,而无需特别进入控制器的深度。 By following this convention, we will always know that the controller will start executing the business logic right where activate() is called. 通过遵循此约定,我们将始终知道控制器将在调用activate()位置开始执行业务逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM