如何在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']);
一个控制器示例,例如我想得到的是myAppModule3'在app.js
,
define(['app'], function (app) {
var myAppModule = angular.module('myAppModule3');
myAppModule.controller('welcomeController', ['$scope', function($scope) {
//your minsafe controller
$scope.message = "Message from WelcomeController";
}]);
});
答案 0 :(得分:1)
您可以更改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";
}]);
});
答案 1 :(得分:1)
通用(非Angular特定)方式是使用对象:
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
。