Javascript 角度+;Requirejs-如何返回多个模块?

Javascript 角度+;Requirejs-如何返回多个模块?,javascript,angularjs,module,requirejs,Javascript,Angularjs,Module,Requirejs,如何在requirejs环境中返回多个角度模块 这是我的app.js define([ 'angular', 'angular-route', 'jquery' ], function (ng,ngRoute,$) { 'use strict'; console.log($('h1').length); return ng.module('myApp', ['ngRoute']); }); 我还需要几个模块返回 ng.module('my

如何在requirejs环境中返回多个角度模块

这是我的app.js

define([
    'angular',
    'angular-route',
    'jquery'
], function (ng,ngRoute,$) {
    'use strict';
    console.log($('h1').length);

    return ng.module('myApp', ['ngRoute']); 

}); 
我还需要几个模块返回

ng.module('myAppModule1', ['ngRoute']); 
ng.module('myAppModule2', ['ngRoute']); 
ng.module('myAppModule3', ['ngRoute']); 
一个控制器示例,例如,我想在
app.js
中获取“myAppModule3”

define(['app'], function (app) {
    var myAppModule = angular.module('myAppModule3');
    myAppModule.controller('welcomeController', ['$scope', function($scope) { 
        //your minsafe controller 
        $scope.message = "Message from WelcomeController"; 
    }]);
});

您可以更改
app.js
以返回其字段为模块的对象:

define([
    'angular',
    'angular-route',
    'jquery'
], function (ng,ngRoute,$) {
    'use strict';
    console.log($('h1').length);

    return {
        myApp: ng.module('myApp', ['ngRoute']),
        myAppModule1: ng.module('myAppModule1', ['ngRoute']), 
        myAppModule2: ng.module('myAppModule2', ['ngRoute']), 
        myAppModule3: ng.module('myAppModule3', ['ngRoute'])
    };
}); 
并按如下方式更改控制器:

define(['app'], function (app) {
    app.myAppModule3.controller('welcomeController', ['$scope', function($scope) { 
        //your minsafe controller 
        $scope.message = "Message from WelcomeController"; 
    }]);
});
通用(非角度特定)方法是使用对象:

 return {module1: /*..*/, module2: /*...*/ };
然后您只需访问以下值:

 define(['app'], function (app) {
    var module1 = app.module1;
 });
但是,在Angular中,您刚刚在Angular全局中注册了“myAppModule1”。无需执行对象返回,您可以使用角度对象检索已注册的模块:

 define(['angular'], function (angular) {

    var module1 = angular.module('myAppModule1');
    // without extra parameter it tells angular to retrive an existing
    // module
 });

更新:我刚刚意识到您在代码中做到了这一点。没用?可能您有依赖性问题,请确保先加载
app.js

谢谢。但是我得到了这个错误
TypeError:app.controller不是一个函数app.controller('HomeCtrl',['$scope',function($scope){
…谢谢。但是它不起作用。我得到了一个错误-
TypeError:app.controller不是一个函数app.controller('HomeCtrl',['$scope',function($scope)){
…您必须调用
app.myAppModule.controller
,而不是
app.controller
myAppModule
从何而来?请查看
app.js
中的
return
语句。该模块是一个具有四个字段的对象,分别名为
myApp
myAppModule1
myAppModule2
、和
myAppM>odule3
。抱歉。我输入了一个拼写错误,这被带入了我的评论中,直到现在我才看到。它是
app.myAppModule3
(带有3)这是你需要的。我知道你已经将问题编辑回原来的状态。这是正确的做法。否则,你会有麻烦。你所问的新问题是一个不同的问题,需要不同的问题。简言之,你创建了两次模块:一次是使用
ng app
指令,一次是使用
ng.模块
呼叫。感谢路易斯的回复!:D