saravana_pc
saravana_pc

Reputation: 2687

Angular - define modules in multiple files

I have the following in app.js

config = angular.module('config', [])
.constant('Constants', {
        Car: 'BMW', 
        Phone: 'G4'
});

services = angular.module('services', ['config']);
controllers = angular.module('controllers', ['config', 'services']);
app = angular.module('myApp', ['config', 'controllers']);

I want to move the "config" module definition to a separate file as I'm expecting it to grow bigger.

Is it possible to move the below portion to a separate file:

config = angular.module('config', [])
.constant('Constants', {
        Car: 'BMW', 
        Phone: 'G4'
});

Upvotes: 1

Views: 593

Answers (2)

Deblaton Jean-Philippe
Deblaton Jean-Philippe

Reputation: 11398

There is no problem with doing that.

You just need to make sure the config module will be loaded before the app module

Upvotes: 1

sonnenhaft
sonnenhaft

Reputation: 1647

You've chosen very bad structure for your angular app. Please check this url https://scotch.io/tutorials/angularjs-best-practices-directory-structure

And about your question. Instead of using caching variables just do next thing:

file0: angular.module('config',[]);
file1: angular.module('config').constant('Constants', {Car: 'BMW', Phone: 'G4'});
file2: angular.module('config').constant('Constants2', {Car: 'BMW', Phone: 'G4'});

Upvotes: 0

Related Questions