AngularJS控制器和"使用严格"

Chr*_*ier 28 use-strict jshint angularjs

我最近开始使用JSHint,它要求我使用"use strict"的函数形式.从那以后,AngularJS抛出一个错误:

"错误:参数'webAddressController'不是函数,未定义"

当我删除"use strict"的函数形式时,控制器加载正常.

控制器:

(function () {
    "use strict";

    function webAddressController($scope, $rootScope, web_address_service) {
             // Do things
    }

}());
Run Code Online (Sandbox Code Playgroud)

有没有人对这里发生的事情有任何见解?

Ben*_*esh 44

首先,我想声明pkozlowski真的知道他在Angular的东西,但这实际上不是一个Angular问题,因为它是一个关闭的问题.

Angular正在两个地方寻找控制器:

  1. 在自己的通过Module.controller()注册的控制器注册表中
  2. 全局变量(或全局函数声明)中

问题是你的闭包内"使用严格"的一切都不是全局的.它被包含并在包含它的匿名函数中私有化.

(function() {
   // nothing in here is global or even public.
   // "use strict" or not.

   "use strict"; // this is mostly irrelevant.

   // this will not work, because it's wrapped and not global
   function ThisDoesntWork($scope) {
   };

   // window is the global root variable. So this works.
   window.ThisWorks = function($scope) {

   };

   // this will work, because it's explicitly registering the controller
   // presuming app is your Module variable from outside of the closure.
   app.controller('ThisIsBest', function($scope) {

   });

})();

//this works because it's global.
function ThisAlsoWorks($scope) {

}

// if you declare a global var, then set it inside
// of your closure, you're good to go too.
var ThisWillWorkToo;

(function {
    //here we're setting it again.
    ThisWillWorkToo = function($scope) {
    };
})();


// if you're really crazy you can even do this...
 var ThisWillWorkButItsWeird = (function() {
      "use strict";

       function ThisWillWorkButItsWeird($scope) {

       }

       return ThisWillWorkButItsWeird;
  })();
Run Code Online (Sandbox Code Playgroud)

在一天结束时,您可以在任何函数内部使用"use strict",如果您愿意,可以在文件级别."使用严格"本身并不会破坏任何东西.正如您所看到的,有一千种注册控制器的方法.最好的选择可能是按照建议用.controller方法显式注册它们.


pko*_*rce 14

我想JSHint试图告诉你这里是为了避免全局变量(这显然是一个非常好的做法!).

AngularJS对解决同一问题(即避免全局变量)有不同意见,并允许您在模块中定义控制器(使用全局angular命名空间).您可以使用以下模块重写您的示例:

angular.module('myApp',[]).controller('webAddressController', function($scope) {
    // Do things
});
Run Code Online (Sandbox Code Playgroud)

以下是jsFiddle在实践中说明这一点:http://jsfiddle.net/t3vBE/1/

使用此方法,您不会使用控制器构造函数来污染全局命名空间.

angular如果要使用严格模式,则需要更改JSHint配置以允许全局变量.或者,您也可以将整个代码(再次使用模块)包装到一个可以执行的函数中:

(function () {
    "use strict";

angular.module('myApp',[]).controller('webAddressController', function($scope) {

    $scope.name = 'World';
    // Do things
});

}());?
Run Code Online (Sandbox Code Playgroud)

这是jsFiddle:http://jsfiddle.net/t3vBE/4/

对我来说,这只有在你想要定义纯JavaScript,"辅助"函数时才有意义,否则我会依赖AngularJS服务.

  • jshint警告并不完全是为了防止全局变量,而是为了防止类似的全局"use strict"问题.如果将文件连接在一起(常见的构建步骤),则顶级"use strict"将应用于所有后续文件(即使它们未编写为符合jshint.)函数形式仅将jshint限制为该函数. (2认同)