javascript - jslint 错误 : Unexpected expression 'use strict' in statement position

标签 javascript angularjs jslint

<分区>

当我尝试在 sublime text 中保存以下代码时,

'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

最佳答案

您不能在 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";
   });
})();

关于javascript - jslint 错误 : Unexpected expression 'use strict' in statement position,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41349429/

相关文章:

javascript - Angular - 在 forEach 循环中等待 API 调用,然后返回数组

javascript - JSLint 错误 "A leading decimal point can be confused with a dot"

javascript - javascript 中的概要

javascript - jslint 忽略预期的 '{' 错误

javascript - angularjs 1.5 中的等待功能

javascript - Kendo UI 开关控制与 AngularJS

javascript - webview中的远程网站可以与Phonegap结合使用吗?

javascript - 在 Firefox 中速度测试 Math() 时出现奇怪的结果

javascript - 要求未找到 Angular Directive(指令) Controller

css - 使用 ng-show 时如何防止表格重新调整大小?