None
None

Reputation: 5670

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?

Upvotes: 7

Views: 4076

Answers (1)

Shashank Agrawal
Shashank Agrawal

Reputation: 25797

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.)

Upvotes: 14

Related Questions