1
我正在尋找一種將多個模塊的某些配置「發送」或「註冊」到單個模塊的方法。如何在AngularJS中將配置從一個模塊推送到另一個模塊
這個想法是,不同的模塊將擁有自己的路由配置。單個模塊控制器將根據這些配置構建菜單。
我想「發送」或「註冊」配置而不是查詢,因爲菜單控制器無法知道哪些模塊可用。
我正在尋找一種將多個模塊的某些配置「發送」或「註冊」到單個模塊的方法。如何在AngularJS中將配置從一個模塊推送到另一個模塊
這個想法是,不同的模塊將擁有自己的路由配置。單個模塊控制器將根據這些配置構建菜單。
我想「發送」或「註冊」配置而不是查詢,因爲菜單控制器無法知道哪些模塊可用。
這裏是我的需求的解決方案:
每個模塊使用$ 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;
}]);
你可能會增加一些檢查,以確保顯示名設置,等等