【发布时间】:2015-04-27 08:49:40
【问题描述】:
我正在寻找一种将一些配置从多个模块“发送”或“注册”到单个模块的方法。
这个想法是不同的模块将有自己的路由配置。一个单一的模块控制器将基于这些配置构建菜单。
我想“发送”或“注册”配置而不是查询,因为菜单控制器无法知道哪些模块可用。
【问题讨论】:
我正在寻找一种将一些配置从多个模块“发送”或“注册”到单个模块的方法。
这个想法是不同的模块将有自己的路由配置。一个单一的模块控制器将基于这些配置构建菜单。
我想“发送”或“注册”配置而不是查询,因为菜单控制器无法知道哪些模块可用。
【问题讨论】:
这是我需要的解决方案:
每个模块使用 $routeProvider 定义自己的路由,并使用自定义参数定义路由是否应显示在菜单中,以及另一个带有要显示名称的自定义参数。 (参见上面的模块 A 示例)。
然后菜单控制器将循环 $route.routes 变量(包含所有定义的路由)来构建菜单。 (参见上面的模块 B 示例)。
模块 A:
angular
.module('module-a', ['ngRoute'])
.config(function ($routeProvider) {
$routeProvider
.when('/path-a', {
controller: 'ModuleAController',
templateUrl: 'path/to/template.html',
menuDisplay: true, // Custom parameter
displayName: 'Solution Manager' // Custom parameter
});
});
模块 B:
angular
.module('module-b', [])
.controller('MenuController', ['$scope', '$route', function ($scope, $route) {
var entries = [];
angular.forEach($route.routes, function (route, path) {
if ( route.menuDisplay == true ) {
this.push({
displayName: route.displayName,
route: '#' + path
});
}
}, entries);
$scope.menu = entries;
}]);
您可能会添加更多检查以确保提供 displayName 等...
【讨论】: