[英]Using jshint with AngularJS and Controllers
我正在嘗試將JSHint合並到我的構建過程中。 我正在構建的應用程序使用AngularJS。 目前,我有一個不確定的解決方法,我有一個沖突。 構建應用程序時,出現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.
我收到此錯誤,因為在我的.jshintrc文件中,我具有以下內容:
“ predef”:[“ angular”,“ myApp”],
如果刪除“ myApp”,則會收到另一個錯誤,提示:
src\app\account\welcome.html.js
3 |myApp.controller('WelcomeController', ['$scope', function($scope) {
^ 'myApp' is not defined.
1 error in 2 files
之所以定義這樣的控制器,是因為根據AngularJS文檔 ,您不應在全局范圍內定義控制器。 如您所見,就像我被該死,否則我被該死。 在構建過程中仍包含JSHint的同時,我如何遵循AngularJS的最佳建議?
謝謝!
我認為您可以使用.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
}
}
為什么要完全定義myApp
變量? 我用這種方式定義我的應用程序,控制器等。 您可以鏈接多個控制器,工廠等:
angular.module('myApp',[])
.controller("MainCtrl", function($scope) {
$scope.person = {
nfname: 'John',
lname: 'Deighan',
email: 'john.deighan@gmail.com',
};
});
最后一個給出的示例很好,但是如前所述,如果您調用.module()並將空數組作為第二個參數,則將重新定義模塊依賴性。 為了避免這種情況,只需將模塊名稱傳遞給.module()
angular.module('myApp')
.controller("MainCtrl", function($scope) {
$scope.person = {
nfname: 'John',
lname: 'Deighan',
email: 'john.deighan@gmail.com',
};
});
因為你設置myApp
作為PREDEF .jshintrc
文件,jshint認為myApp
已經在這個項目目錄中的所有文件進行定義。 因此,當您定義myApp
,將重新定義錯誤。
因為在同一文件中沒有myApp
的定義。 我猜你在app.js
文件中定義它。 如您所見,在Angularjs文檔中,它們的控制器均在myApp
之后定義,它們位於同一文件中。
不需要在.jshint
中將myApp
設置為.jshint
或global,建議在定義控制器或配置時執行以下操作:
angular.module('myApp')
.controller('myController',
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.