AngularJS and RequireJS - Uncaught Error: No module: MyApp

AngularJS and RequireJS - Uncaught Error: No module: MyApp

本文关键字:No module MyApp Error Uncaught and RequireJS AngularJS      更新时间:2023-09-26

我想用requireJS创建一个新的angular项目。

这里是我的index.html:

<!doctype html>
<html ng-app="MainApp">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script data-main="js/requirejs.config.js" src="common/lib/requirejs/require.js"></script>
    <title>{{'TITLE' | translate}}</title>
    <link rel="stylesheet" href="common/lib/bootstrap-3.1.1-dist/css/bootstrap.css">
    <link rel="stylesheet" href="css/app.css">
</head>
<body ng-controller="MainCtrl" ng-cloak class="ng-cloak">
    <button type="button" class="btn btn-default btn-lg">
        <span class="glyphicon glyphicon-star"></span>{{'HELLO' | translate}}
    </button>
</body>
</html>

下面是我的requirejs.config.js:

require.config({
baseUrl: 'common',
paths: {
    angular         : 'lib/angular-1.2.19/angular',
    jquery          : 'lib/jquery/jquery-2.1.1',
    uibootstrap     : 'lib/ui-bootstrap-0.11.0/ui-bootstrap-tpls-0.11.0',
    bootstrap       : 'lib/bootstrap-3.1.1-dist/js/bootstrap',
    app             : '../js/app',
    config          : '../js/config'
},
shim : {
    angular : {
        exports: 'angular'
    },
    jquery : {
        exports: 'jquery'
    },
    uibootstrap : {
        exports: 'uibootstrap'
    },
    bootstrap : {
        exports: 'bootstrap'
    }
},
// To remove urlArgs on production
urlArgs: "v=" +  (new Date()).getTime() * Math.random()
});
require([
'../js/MainCtrl'
]);

在加载了index.html的浏览器上每按第二次F5,我收到错误:

"[$injector:nomod] Module 'MainApp' 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.

知道为什么吗?

p。S -删除ng-app="MainApp"标签并在dom加载后手动引导angular解决它,但我担心我做错了什么,想确保

编辑-这是我的app.js:

define(["angular", "config"], function(angular) {
    return angular.module('MainApp', ['common']);
});
这是我的config.js:
define([
"angular",
"constants/constants",
"values/values",
"filters/filters",
"services/services",
"factories/factories",
"directives/directives",
"providers/providers",
"controllers/controllers"
], function(angular) {  
return angular.module('common', [
    'common.factories',
    'common.services',
    'common.directives',
    'common.providers',
    'common.constants',
    'common.values',
    'common.controllers',
    'common.filters'
]);
});

检查父文件夹名称中是否有空格字符,就我的情况而言-我的文件夹结构像…/进步/AngularSample/

我有同样的问题,显然在代码中没有问题,我最终将parentfolder名称从"in Progress"更改为"in -Progress",它开始正常工作!