简体   繁体   中英

Inject modules conditionally in AngularJS app

My Angular app structure is like this:

App.js

angular.module('RateRequestApp', [
    'RateRequestApp.services',
    'RateRequestApp.controllers',
    'ui.bootstrap',
    'angular-loading-bar',
    'textAngular',
    'angularFileUpload'
]);

I am using different HTML files for different pages and I am not using Angular's $route , but still I want to use same app in all pages with different controllers.

As you can see I am injecting third party modules to my app. The problem is that in some pages I don't want some of this modules, How can I avoid them where I don't need them?

Yes you can do it something like this:

var myApp = angular.module('RateRequestApp', [
    'RateRequestApp.services',
    'RateRequestApp.controllers',
    'ui.bootstrap',
    'angular-loading-bar'
]);

var lazyModules = ['textAngular', 'angularFileUpload'];

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

In this case you may inject the modules conditionally. (But please note, lazy loading of some module may not work where there is some configuration needed.)

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