gpt4 book ai didi

javascript - 错误 : [$injector:nomod] Module '' is not available

转载 作者:行者123 更新时间:2023-11-30 00:14:49 24 4
gpt4 key购买 nike

这是我的配置文件 karma.conf.js 的一部分:

    // base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '../',

// list of files / patterns to load in the browser
files: [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-route/angular-route.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/js/app.js',
'app/js/controllers/*.js',
'app/js/services/*.js',
'test/unit/*.js'
],

这是我的文件夹结构:

folder's structure

这是 app.js 文件:

/**
* Created by Pietro on 30/12/15.
*/
'use strict';

var drfmApp = angular.module('drfmApp', [
'ngRoute','drfmControllers','ngCookies'
]);


drfmApp.config(['$routeProvider','$locationProvider',
function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'views/login.html',
controller: 'LoginController'
}).
when('/login', {
templateUrl: 'views/login.html',
controller: 'LoginController',
}).
when('/register', {
templateUrl: 'views/register.html',
controller: 'RegisterController',
}).
when('/dashboardESCO', {
templateUrl: 'views/dashboard_esco.html',
controller: 'DashboardESCOControllers'
}).
when('/dashboardRetailer', {
templateUrl: 'views/dashboard_retailer.html',
controller: 'DashboardRetailerControllers'
}).
when('/dashboardAggregator', {
templateUrl: 'views/dashboard_aggregator.html',
controller: 'DashboardAggregatorControllers'
}).
otherwise({
redirectTo: '/login'
});
}]);

drfmApp.run(['$rootScope', '$location', '$cookieStore', '$http',
function($rootScope, $location, $cookieStore, $http) {
// keep user logged in after page refresh
$rootScope.globals = $cookieStore.get('globals') || {};
if ($rootScope.globals.currentUser) {
$http.defaults.headers.common['Authorization'] = 'Basic ' + $rootScope.globals.currentUser.authdata; // jshint ignore:line
}

$rootScope.$on('$locationChangeStart', function (event, next, current) {
// redirect to login page if not logged in and trying to access a restricted page

// $ è jQuery
var restrictedPage = $.inArray($location.path(), ['/login', '/register']) === -1;
var loggedIn = $rootScope.globals.currentUser;
if (restrictedPage && !loggedIn) {
console.log('Run Config: User not logge in or trying to access a restricted web page')
$location.path('/login');
}
});
}]);

我正在尝试像这样运行一个简单的单元测试文件(使用 Karma 和 Jasmine):

'use strict';

/* jasmine specs for controllers go here */
describe('Drfm controllers', function () {

beforeEach(module('drfmControllers'));

it('DUMMY TEST', function () {
expect(3).toBe(3);
});

});

其中 drfmApp 是 app.js 文件中主模块的名称

这是定义 drfmControllers 的地方(在 controllers.js 中):

/**
* Created by Pietro on 07/01/16.
*/
'use strict';

var drfmControllers = angular.module('drfmControllers', []);


drfmControllers.controller('DashboardESCOControllers', ['$scope',

function($scope) {
$scope.dashboard = "ESCO Dashboard";
}

]);

drfmControllers.controller('DashboardRetailerControllers', ['$scope',

function($scope){
$scope.dashboard="Retailer Dashboard";}

]);

drfmControllers.controller('DashboardAggregatorControllers', ['$scope',

function($scope){
$scope.dashboard="Aggregator Dashboard";}

]);

但我总是有这个错误:

    Error: [$injector:nomod] Module 'drfmControllers' is not available! You either misspelled the module name or forgot to load it. 
If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.4.8/$injector/nomod?p0=drfmControllers at /Users/Pietro/repos/drfmcockpit/app/bower_components/angular/angular.js:68

我在 karma 配置文件中读到了 *.js 文件的顺序:但似乎是根据这个规则[参见问题开头的"file"]。

谢谢大家


更新解决方案:更改 conf 文件:

    // list of files / patterns to load in the browser
files: [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-route/angular-route.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/js/app.js',
'app/js/controllers/controllers.js',
'app/js/controllers/LoginController.js',
'app/js/controllers/RegisterController.js',
'test/unit/*.js'
],

运行良好。似乎在文件检查中。我不太清楚..

最佳答案

您还没有在配置选项中设置 basePathBy default ,它等于 ''。反过来,空字符串作为 basePath 值,使其相对于配置文件目录。据描述here :

If the basePath is a relative path, it gets resolved to the directory where the configuration file is located

这意味着您应该将 basePath 值设置为绝对路径,或者您应该将文件路径更改为相对路径,例如:

files: [
'../app/bower_components/angular/angular.js',
'../app/bower_components/angular-route/angular-route.js',
'../app/bower_components/angular-mocks/angular-mocks.js',
'../app/js/app.js',
'../app/js/controllers/*.js',
'../app/js/services/*.js',
'unit/*.js'
],

关于javascript - 错误 : [$injector:nomod] Module '<name_of_module>' is not available,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35135995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com