How to create a pluggable application in angular?

后端 未结 1 1688
天涯浪人
天涯浪人 2020-12-12 02:34

I want to create an application which can be divided in multiple module with their own routing and all. And user can turn on and off these modules from application main modu

相关标签:
1条回答
  • 2020-12-12 03:28

    The list of enabled modules should be provided for main module:

    var enabledModules = [...];
    
    angular.module('app', ['thirdParty', 'app.common'].concat(enabledModules));
    

    Obviously, enabledModules array can't be normally loaded with $http, because the application isn't bootstrapped at this point. XHR or server-side templating may be be used to define it.

    Alternatively, a separate application can be used to load prerequisites. Due to the use of DI, it can be thoroughly tested.

    angular.module('app', ['thirdParty', 'app.common']);
    
    angular.module('appInitializer', [])
    .factory('loader', ($http) => {
      return $http.get('enabled-modules').then((result) => result.data);
    })
    .factory('initializer', (loader, $document) => {
      return loader.then((enabledModules) => {
        $document.ready(() => {
          angular.bootstrap($document.find('body'), ['app'].concat(enabledModules));
        });
      });
    });
    
    angular.injector(['ng', 'appInitializer'])
    .get('initializer')
    .catch((err) => console.error(err));
    
    0 讨论(0)
提交回复
热议问题