【问题标题】:AngularJS getting "Missing 'use strict' statement"-errors, even tough I've included the statementsAngularJS 得到“缺少 'use strict' 语句”-错误,即使我已经包含了这些语句
【发布时间】:2015-10-30 10:21:47
【问题描述】:

我有一个控制器和两个模块,分为三个文件。当我在 Grunt 中运行 Jshint-Task 时,我总是收到错误消息,即缺少“use strict”语句。所以为了修复这个错误,我将语句包含在这三个文件中,但我仍然得到错误,我不明白为什么。由于我仍在学习 Angular 的基础知识,所以我不知道为什么会发生这种情况。

感谢您的回答!

控制器:

"use strict";
angular.module('calculatorApp').controller('HomeController', ['calculatorApp', function(){
    var $this = this;

    $this.test = function () {
        $this.alert('test');
    };
}]);

模块:

"use strict";
    var  calculatorApp = angular.module('calculatorApp', ['ngRoute']);
    calculatorApp.config(function($routeProvider){

        $routeProvider
        .when('/', {
                controller: 'HomeController',
                templateUrl: 'home.tpl.html'
            })
        .otherwise({
                redirectTo: '/'
            })
    });

【问题讨论】:

  • 请发布示例代码
  • '使用严格';不适合你?请出示 plunker 或更多代码
  • 我现在添加了一些代码示例。

标签: javascript angularjs jshint


【解决方案1】:

始终包装您的模块以避免在 concat 和 miniziming 任务中出错(避免变量冲突和泄漏到全局范围),还包括 use strict 在此函数中。

例子:

app.js

var app;
(function (app) {
    'use strict';
    angular.module('app', [
        'module1',
        'module2',
        'moduleX'
    ]);
})(app || (app = {}));

randomcontroller.js

var app;
(function (app) {
    'use strict';
    var RandomController = (function () {
        function RandomController(logger) {
            this.logger = logger;
            this.title = 'RandomView';
            this.logger.info('Activated Random View');
        }
        RandomController.$inject = ['logger'];
        return RandomController;
})();
angular.module('app').controller('RandomController', RandomController);
})(app || (app = {}));

如果有任何问题,请参考令人惊叹的 John Papa 指南 -> https://github.com/johnpapa/angular-styleguide

【讨论】:

  • 感谢您对正确调整我的代码的意见,但恐怕它不能解决错误。
【解决方案2】:

我想分享我的错误解决方案。看起来我的 src 文件夹中有一个供应商文件夹。如果其他人遇到此错误,请检查您的文件夹结构!

【讨论】:

    猜你喜欢
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 2020-12-20
    • 2013-04-27
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多