你好,我正在尝试使用 Angular 开发一个网络应用程序。我已将 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);
……
当我运行应用程序时,我收到此错误:
- 错误:[ng:areq] 参数“MainController”不是函数,未定义
- 未捕获错误:[$injector:nomod] 模块“appApp.main”不可用!您要么拼错了模块名称,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。
如何修复该错误?谢谢
最佳答案
第一件事是:
在你编写的html中
ng-app="appApp"
但是在您编写的模块定义中
angular
.module('appApp.main', ['ui.router'])
模块名称应该相同,除非您有另一个 appApp 模块并且添加“appApp.main”模块作为依赖项。
另一件事是,当您使用“ui-router”时,您需要将 html 中的 ui-router 的 js 库文件与 Angular 库文件链接起来。
只需检查js文件的顺序即可。首先是 Angular ,然后是所有库 js,然后是应用程序、主、主 Controller
关于javascript - 无法加载模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32285045/