简体   繁体   English

在AngularJS和控制器中使用jshint

[英]Using jshint with AngularJS and Controllers

I am trying to incorporate JSHint into my build process. 我正在尝试将JSHint合并到我的构建过程中。 The app that I'm building uses AngularJS. 我正在构建的应用程序使用AngularJS。 Currently, I have a conflict that I'm not sure how to resolve. 目前,我有一个不确定的解决方法,我有一个冲突。 When I build my app, I get a JSHint error that says: 构建应用程序时,出现JSHint错误,提示:

   src\app\app.js
      3 |var myApp = angular.module('myApp', []);
             ^ Redefinition of 'myApp'.

>> 1 error in 2 files
Warning: Task "jshint:source" failed. Use --force to continue.

I get this error because in my .jshintrc file, I have the following: 我收到此错误,因为在我的.jshintrc文件中,我具有以下内容:

"predef": ["angular", "myApp"], “ predef”:[“ angular”,“ myApp”],

If I remove "myApp", I get a different error that says: 如果删除“ myApp”,则会收到另一个错误,提示:

src\app\account\welcome.html.js
   3 |myApp.controller('WelcomeController', ['$scope', function($scope) {
      ^ 'myApp' is not defined.

1 error in 2 files

The reason I am defining a controller like that is because according to the AngularJS documentation , you should not define controllers in the global scope. 之所以定义这样的控制器,是因为根据AngularJS文档 ,您不应在全局范围内定义控制器。 As you can see, its like I'm damned if I do, and damned if I don't. 如您所见,就像我被该死,否则我被该死。 How do I follow the AngularJS best recommendations while still including JSHint in my build process? 在构建过程中仍包含JSHint的同时,我如何遵循AngularJS的最佳建议?

Thank you! 谢谢!

I think you can fix this with a globals key in the .jshintrc file 我认为您可以使用.jshintrc文件中的全局密钥来解决此问题

{
    "node": true,
    "browser": true,
    "esnext": true,
    "bitwise": true,
    "camelcase": true,
    "curly": true,
    "eqeqeq": true,
    "immed": true,
    "indent": 4,
    "latedef": true,
    "newcap": true,
    "noarg": true,
    "quotmark": "single",
    "undef": true,
    "unused": true,
    "strict": true,
    "trailing": true,
    "smarttabs": true,
    "multistr": true,
    "globals": {
        "myApp": false
    }
}

Why define a myApp variable at all? 为什么要完全定义myApp变量? I define my app, controller, etc. this way. 我用这种方式定义我的应用程序,控制器等。 You can chain multiple controllers, factories, etc.: 您可以链接多个控制器,工厂等:

angular.module('myApp',[])

.controller("MainCtrl", function($scope) {
    $scope.person = {
        nfname: 'John',
        lname: 'Deighan',
        email: 'john.deighan@gmail.com',
    };
});

The last given example is good, but as said if you call .module() with an empty array as a second argument you will redifine your module dependencies. 最后一个给出的示例很好,但是如前所述,如果您调用.module()并将空数组作为第二个参数,则将重新定义模块依赖性。 To avoid that, only pass the module name to .module() 为了避免这种情况,只需将模块名称传递给.module()

angular.module('myApp')

.controller("MainCtrl", function($scope) {
    $scope.person = {
        nfname: 'John',
        lname: 'Deighan',
        email: 'john.deighan@gmail.com',
    };
});

Reasons: 原因:

Why redefinition? 为什么要重新定义?

Because you set myApp as predef in the .jshintrc file, jshint thinks myApp is already be defined in all the files in this project directory. 因为你设置myApp作为PREDEF .jshintrc文件,jshint认为myApp已经在这个项目目录中的所有文件进行定义。 So when you define myApp , error redefinition. 因此,当您定义myApp ,将重新定义错误。

Why undefined? 为什么未定义?

Because there're not myApp 's definition in the same file. 因为在同一文件中没有myApp的定义。 I guess you define it in app.js file. 我猜你在app.js文件中定义它。 As you noticed, in the Angularjs documentation, their controller is all defined after the myApp , they are in the same file. 如您所见,在Angularjs文档中,它们的控制器均在myApp之后定义,它们位于同一文件中。

Solution: 解:

Don't need set myApp as predef or global in .jshint , I recommend that when you defined a controller or config, do it like this: 不需要在.jshint中将myApp设置为.jshint或global,建议在定义控制器或配置时执行以下操作:

angular.module('myApp')
.controller('myController', 

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

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