简体   繁体   中英

How to bootstrap modules in Angular 2+ with server side response

I used to bootstap my Angular JS app with a call to the server to get the modules a user has access, I can't figure how in Angular 2+

This is a AngularJS exemple

I used ngMockE2E only for the mock exemple.

  (function (ngNode) { angular.module("boostrapApp", ['ngMockE2E']).service('clientModule', function ($http) { this.get = function () { return $http.get('modules.json'); } }).run(function ($httpBackend) { $httpBackend.whenGET('modules.json').respond(function(method, url, data) { return [200, ['m1', 'm2', 'm3'], {}]; }); }); angular.bootstrap(ngNode, ['boostrapApp']); angular.element(ngNode).injector().get('clientModule').get().then(function (modules) { angular.module('app', modules.data); angular.module('app').run(['$log', function ($log) { $log.info('run app'); }]); angular.bootstrap(document.body, ['app']); }); })(document.createElement('div')); angular.module('m1', []).run(['$log', function ($log) { $log.info('run m1'); }]); angular.module('m2', []).run(['$log', function ($log) { $log.info('run m2'); }]); angular.module('m3', []).run(['$log', function ($log) { $log.info('run m3'); }]); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-mocks/1.6.9/angular-mocks.js"></script> 

您可以使用动态内容加载器

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM