【问题标题】:How to push configuration from one module to another module in AngularJS如何在AngularJS中将配置从一个模块推送到另一个模块
【发布时间】:2015-04-27 08:49:40
【问题描述】:

我正在寻找一种将一些配置从多个模块“发送”或“注册”到单个模块的方法。

这个想法是不同的模块将有自己的路由配置。一个单一的模块控制器将基于这些配置构建菜单。

我想“发送”或“注册”配置而不是查询,因为菜单控制器无法知道哪些模块可用。

【问题讨论】:

    标签: angularjs configuration


    【解决方案1】:

    这是我需要的解决方案:

    每个模块使用 $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 等...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2016-09-30
      • 1970-01-01
      • 2016-08-28
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多