AngularJS得到“;缺少'使用strict'语句“-错误,甚至是艰难的I'We包括了这些陈

AngularJS getting "Missing 'use strict' statement"-errors, even tough I've included the statements

本文关键字:We 包括了 strict 使用 缺少 得到 语句 AngularJS 错误      更新时间:2023-09-26

我有一个控制器和两个模块,分成三个文件。当我在Grunt中运行Jshint任务时,我总是会收到错误,即缺少"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: '/'
            })
    });

Allways包装模块以避免concat&miniziming任务(避免变量冲突和泄漏到全局范围),还包括在该函数中使用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

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