Home Selectively use dependent modules in AngularJS
Reply: 1

Selectively use dependent modules in AngularJS

user2340939 Published in 2017-12-07 20:54:21Z

I'm writing a small application where I have all AngularJS code inside of one file. This file is included in every HTML page. How would I filter which dependent modules are needed for specific HTML page? I.e. if I have e.g.:

var app = angular.module('MyApp', ['ngTouch', 'ngMessages']);

but I only need ngTouch on some pages and on the other pages only ngMessages, how would I prevent loading the other, unnecessary dependent module for each page?

alexhuang Reply to 2017-12-07 22:12:36Z

You would use specialized modules to depend on a core module to separate the concerns. So for example, you could have something similar to this:

angular.module('app.core', ['ngAnimate']);
angular.module('PicadviceApp', ['app.core', 'ngTouch']);
angular.module('messages', ['app.core', 'ngMessages']);

This would essentially declare the 'app.core' module as the core module that every page in your application needs to load, and on specific pages you would then have specialized modules that load any additional dependencies.

One page can load up the PicadviceApp module and another page would load up the messages module. But both pages would have access to the ngAnimate module because of the app.core dependency.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.327163 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO