Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 977
None Avatar asked Dec 29 '14 10:12

None


1 Answers

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

like image 168
Shashank Agrawal Avatar answered Sep 30 '22 13:09

Shashank Agrawal