2016-12-27 52 views
2

當我嘗試保存崇高的文字下面的代碼,JSLint的錯誤:意外表達的意見書「使用嚴格的」

'use strict'; 
/*global angular,_*/ 

var app = angular.module("myApp", []); 
app.controller("myCtrl", function ($scope) { 
    $scope.firstName = "John"; 
    $scope.lastName = "Doe"; 
}); 

我正在以下的JSLint錯誤:

#1 Unexpected expression 'use strict' in statement position. 
    'use strict'; // Line 1, Pos 1 
#2 Place the '/*global*/' directive before the first statement. 
    /*global angular,_*/ // Line 2, Pos 1 
#3 Undeclared 'angular'. 
    var app = angular.module("myApp", []); // Line 4, Pos 11 
#4 Expected 'use strict' before '$scope'. 
    $scope.firstName = "John"; // Line 6, Pos 3 
#5 Expected '$scope' at column 5, not column 3. 
    $scope.lastName = "Doe"; // Line 7, Pos 3 
+0

我已經檢查了鏈接@TheSharpieOne。它不符合我的目的。 – Ayan

+0

所以......答案的部分內容是「根據[文檔](http://jslint.com/help.html),JSLint的瀏覽器選項自動禁止在全球範圍內使用」嚴格使用「; 。就我所知,無法重新啓用它。「沒有幫助你意識到你不能把'嚴格使用';'那裏並且把它放在那裏就是問題? – TheSharpieOne

+0

如果沒有使用'嚴格使用',那麼jslint錯誤仍然存​​在,說'預期'在'$ scope'之前使用嚴格'「 – Ayan

回答

2

你不能以jslint的全局方式使用'use strict';。見https://stackoverflow.com/a/35297691/1873485

考慮到這一點,你需要從全局範圍內將其刪除,並在你的函數範圍內添加,或在IFEE包裹一切

/*global angular,_*/ 
var app = angular.module("myApp", []); 
app.controller("myCtrl", function ($scope) { 
    'use strict'; 
    $scope.firstName = "John"; 
    $scope.lastName = "Doe"; 
}); 

或把它包:

/*global angular,_*/ 
(function(){ 
    'use strict'; 
    var app = angular.module("myApp", []); 
    app.controller("myCtrl", function ($scope) { 
    $scope.firstName = "John"; 
    $scope.lastName = "Doe"; 
    }); 
})(); 
+0

是的,但沒有。 JSLint使用錯誤消息處理包裝:「不要將函數文字包裝在parens中。」 – schlenger