【问题标题】:Angularjs load module based on conditionsAngularjs根据条件加载模块
【发布时间】:2017-06-25 22:16:13
【问题描述】:

我正在为我的应用程序使用 angularjs 和 gulp。 这是我的目录结构。

app.js的内容是

'use strict';
angular.module('BlurAdmin', [
    'ngAnimate',
    'ui.bootstrap',
    'ui.sortable',
    'ui.router',
    'ngTouch',
    'ngRoute',
    'ngStorage',
    'MyApp.theme',
    'MyApp.pages'
]).run(function ($rootScope, $sessionStorage) {
    $rootScope.sessionStorage = $sessionStorage;
});

它加载 page.module.js

page.module.js 的内容是

(function () {
    'use strict';

    var myApp = angular.module('MyApp.pages', [
        'ui.router',
    ])
    .config(routeConfig);

    /** @ngInject */
    function routeConfig($urlRouterProvider, baSidebarServiceProvider) {
        $urlRouterProvider.otherwise('/dashboard');
    }

    var lazyModules = [
        'MyApp.pages.dashboard',
        'MyApp.pages.create',
        'MyApp.pages.otherModule'
    ];

    angular.forEach(lazyModules, function (dependency) {
        myApp.requires.push(dependency);
    });
})();

这会将所有模块作为菜单项加载到我的页面中。我想根据登录用户的类型使其有条件。

为此,我在 app.js 的 .run 中使用了 $sessionStorage,但我无法在 pages.module.js 中使用它

请帮助我加载有条件的模块,例如如果角色是管理员,那么只有 create 模块会被加载等等。

谢谢。

【问题讨论】:

    标签: javascript jquery angularjs node.js


    【解决方案1】:

    如果我理解正确你需要require.js 检查角色并转到类似“/admin”的页面。

        $routeProvider.when("/admin", angularAMD.route({
            templateUrl: 'your.html', controller: 'createController',
            controllerUrl: 'create'
        })).when("/other", angularAMD.route({
            templateUrl: 'your.html', controller: 'otherController',
            controllerUrl: 'other'
        }))
    

    https://www.codeproject.com/Articles/808213/Developing-a-Large-Scale-Application-with-a-Single

    【讨论】:

    • 还有另一个子菜单,所以我认为这对我不起作用
    【解决方案2】:

    试试这样的:

    (function () {
      'use strict';
    
      var myApp = angular.module('MyApp.pages', [
        'ui.router',
      ])
      .config(routeConfig)
      .run(lazyModules);
    
      /** @ngInject */
      function routeConfig($urlRouterProvider, baSidebarServiceProvider) {
        $urlRouterProvider.otherwise('/dashboard');
      }
    
      function lazyModules($sessionStorage) {
    
        var userModules = [
          'MyApp.pages.dashboard',
          'MyApp.pages.otherModule'
        ];
    
        var adminModules = [
          'MyApp.pages.create',
          'MyApp.pages.otherModule'
        ];
    
        var isAdmin = $sessionStorage.admin;
        var loadModules = isAdmin ? adminModules : adminModules;
    
        angular.forEach(loadModules, function (dependency) {
          myApp.requires.push(dependency);
        });
      }
    
    })();
    

    【讨论】:

    • routeConfig 函数有多种定义,你怎么称呼它们?并且参数 $sessionStorage 来自您的 routeConfig 函数中的哪个位置?
    • 它加载模块,如果我将 forEach() 放在 lazyModules 函数之外
    猜你喜欢
    • 2020-10-20
    • 2013-08-28
    • 2015-07-15
    • 2015-02-09
    • 2012-10-10
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 2017-01-04
    相关资源
    最近更新 更多