【问题标题】:Failed to load modules加载模块失败
【发布时间】:2015-08-29 10:32:53
【问题描述】:

您好,我正在尝试使用 Angular 开发 Web 应用程序。我已将 ng-app="appApp" 添加到 html 文件和 .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

(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

(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);

...........

当我运行应用程序时出现以下错误:

  1. 错误:[ng:areq] 参数“MainController”不是函数,未定义
  2. 未捕获的错误:[$injector:nomod] 模块“appApp.main”不可用!您要么拼错了模块名称,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。

我该如何解决这些错误?谢谢

【问题讨论】:

  • 您是在使用一些构建系统,还是 yoi 只是在 index.html 中添加了文件作为脚本标签?因为它取决于文件的顺序,所以应用程序必须是第一个控制器第二个
  • @tomastrajan 我使用了 yeoman 全栈生成器,现在我正在尝试编辑客户端

标签: javascript angularjs node.js


【解决方案1】:

第一件事是:

在你编写的 html 中

ng-app="appApp"

但是在你写的模块定义中

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

除非您有另一个 appApp 模块并且您将“appApp.main”模块添加为依赖项,否则模块名称应该相同。

另一件事是,当您使用“ui-router”时,您需要在html中链接ui-router的js库文件以及角度库文件。

只检查js文件的顺序。首先是 angular,然后是所有库 js,然后是 app、main、主控制器

【讨论】:

  • 我有另一个模块 appApp。对不起,我没有添加它。我将编辑我的问题
  • 好的,那就看看js文件的顺序。首先是 angular,然后是所有库 js,然后是 app、main、主控制器。
  • 谢谢。您能否编辑您的答案并填写上面的评论,以便我可以将其标记为答案?
  • 我已经在答案的最后部分添加了序列。
  • 我看到了 app.js 的最后一行
【解决方案2】:

我认为,

1.您应该调用 ng-app="appApp.main" 或

2.您应该首先声明 appApp 模块。您应该替换 main.js 中的一些代码

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

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

另外,删除 main.js 中的 [ui.router]。它已经在 app.js 中声明了

【讨论】:

  • a 已经有一个 appApp 模块。我编辑我的问题。抱歉之前没有提供
  • 好的。你解决了这个问题吗
  • 我发现了问题,但我还想解决这个问题
  • 您的 appApp.main 未在 app.js 中声明。声明它。我编辑我的答案。然后第二个问题已修复
猜你喜欢
  • 2017-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 2014-11-22
相关资源
最近更新 更多