【发布时间】:2014-05-29 09:00:13
【问题描述】:
我正在尝试使用 AngularJs 和 RequireJS 构建一个巨大的模块化 Web 应用程序。这是我要构建的目录:
|--index.html
|--css
|--images
|--libs
| └--angular.js
| └--angular-route.js
| └--require.js
|
|--js
| └--app.js
| └--controller.js
|
└--module
|--user
| |--controller
| | └--index.js (that is controller to list all users)
| | └--add.js (this controller to add new user)
| |--service
| | └--user.js
| └--template
| └--index.tpl.html
| └--add.tpl.html
|
└--photo
|--controller
| └--index.js
| └--add.js
|--service
| └--photo.js
└--template
└--index.tpl.html
└--add.tpl.html
在 app.js 中
'use strict';
var hugeApp = angular.module('hugeApp', [
'ngRoute',
'mainController'
]);
hugeApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/:fullpath', {
templateUrl: 'main.html',
controller: 'RouteController'
}).
otherwise({
templateUrl: 'main.html',
controller: 'RouteController'
});
}]);
对于controller.js
'use strict';
var mainController = angular.module('mainController', []);
mainController.controller('RouteController', ['$scope', '$routeParams', function($scope, $routeParams) {
// I will use url link to find the right sub-controller
// example:
// #/user:index
// #/user:add
/* This is Some code to find moduleId and ControllerId */
var moduleId = 'user';
var controllerId = 'index';
// This is the code by using requireJS to replace
// controller to /module/user/controller/index.js
// and view to /module/user/template/index.html
}]);
现在,我想用 RequireJs 完成替换控制器和视图的代码。我该怎么做?
【问题讨论】:
标签: angularjs requirejs routes modular