简体   繁体   English

加载模块失败

[英]Failed to load modules

Hello i am trying to develop a web app with angular. 您好,我正在尝试使用angular开发一个Web应用程序。 I have added the ng-app="appApp" to the html file and also the .js files. 我已经将ng-app =“ appApp”添加到html文件以及.js文件中。

main.controller.js main.controller.js

(function () {
'use strict';

// register the controller as MainController
angular
    .module('appApp.main')
    .controller('MainController', MainController);

/**
 * @ngdoc function
 * @name appApp.main.provider:MainController
 * @description
 * Provider of the {@link appApp.main.controller:MainController MainController}
 *
 * @param {Service} $scope The scope service to use
 * @param {Service} $http The http service to use
 */

// MainController.$inject = [];

function MainController() {
    var vm = this;
}

})();

main.js main.js

(function () {
'use strict';

// register the route config on the application
angular
    .module('appApp.main', ['ui.router'])
    .config(configMainRoute)

// inject configMainRoute dependencies
configMainRoute.$inject = ['$stateProvider', 'mainMenuProvider'];

// route config function configuring the passed $stateProvider
function configMainRoute($stateProvider, mainMenuProvider) {
    var mainState = {
        name: 'main',
        url: '/',
        authenticate: true,
        templateUrl: 'app/main/main.html',
        controller: 'MainController',
        controllerAs: 'vm'
    };

    $stateProvider.state(mainState);

    mainMenuProvider.addMenuItem({
        name: 'Home',
        state: mainState.name,
        order: 1
    });
}

})();

app.js app.js

(function () {
'use strict';

angular
    .module('appApp', [
        // Add modules below
        'ngCookies',
        'ngResource',
        'ngSanitize',
        'ngMessages',
        'ngMaterial',
        'ui.router',
        'btford.socket-io',
        'appApp.main'
    ])
    .config(appConfig)
    .run(appRun);

........... ...........

When i run the app i get this errors: 当我运行该应用程序时,出现以下错误:

  1. Error: [ng:areq] Argument 'MainController' is not a function, got undefined 错误:[ng:areq]参数'MainController'不是一个函数,未定义
  2. Uncaught Error: [$injector:nomod] Module 'appApp.main' is not available! 未捕获的错误:[$ injector:nomod]模块'appApp.main'不可用! 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. 如果注册模块,请确保将依赖项指定为第二个参数。

How can i fix that errors? 我该如何解决该错误? Thank you 谢谢

The first thing is : 第一件事是:

In the html you've written 在您编写的html中

ng-app="appApp"

But in the module definition you've written 但是在模块定义中,您已经编写了

angular
.module('appApp.main', ['ui.router'])

The module names should be same unless you've another appApp module and you add the "appApp.main" module as dependency. 除非您具有另一个appApp模块并且将“ appApp.main”模块添加为依赖项,否则模块名称应相同。

Another thing is as you've using "ui-router" you need to link the js library file of ui-router in the html along with angular library file. 另一件事是,当您使用“ ui-router”时,需要将html中的ui-router的js库文件与角度库文件链接在一起。

Just check the sequence of js files. 只需检查js文件的顺序即可。 At first angular, then all library js, then app, main, main controller 首先是角度,然后是所有库js,然后是应用程序,主控制器和主控制器

I think, 我认为,

1.You should call ng-app=" appApp.main " or 1.您应该调用ng-app =“ appApp.main ”或

2.You should initially declare appApp module. 2.您应该首先声明appApp模块。 You should replace some code in main.js 您应该替换main.js中的一些代码

angular.module('appApp.main', []);

angular.module('appApp', [
        'ui.router',
        'appApp.main'
    ])...

Also, remove [ui.router] in main.js. 另外,删除main.js中的[ui.router]。 It has declared in app.js 它已在app.js中声明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM