AngularJS 如何将配置从一个模块推送到另一个模块
How to push configuration from one module to another module in AngularJS
我正在寻找一种方法 "send" 或 "register" 从多个模块到一个模块的一些配置。
想法是不同的模块会有自己的路由配置。一个单一的模块控制器将根据这些配置构建菜单。
我想"send"或"register"配置而不是查询,因为菜单控制器无法知道哪些模块可用。
这是满足我需求的解决方案:
每个模块使用 $routeProvider 定义自己的路由,自定义参数定义路由是否应显示在菜单中,另一个自定义参数带有要显示的名称。 (参见上面的模块 A 示例)。
然后菜单控制器将循环 $route.routes 变量(包含所有定义的路由)来构建 de 菜单。 (参见上面的模块 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 等...
我正在寻找一种方法 "send" 或 "register" 从多个模块到一个模块的一些配置。
想法是不同的模块会有自己的路由配置。一个单一的模块控制器将根据这些配置构建菜单。
我想"send"或"register"配置而不是查询,因为菜单控制器无法知道哪些模块可用。
这是满足我需求的解决方案:
每个模块使用 $routeProvider 定义自己的路由,自定义参数定义路由是否应显示在菜单中,另一个自定义参数带有要显示的名称。 (参见上面的模块 A 示例)。
然后菜单控制器将循环 $route.routes 变量(包含所有定义的路由)来构建 de 菜单。 (参见上面的模块 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 等...