1

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'
});
5
  • You can't change constant values.... Commented Aug 13, 2015 at 12:38
  • Can have numerous files..not clear what the exact problem is or what you are asking. Should however be grouping modules as features not components Commented Aug 13, 2015 at 12:40
  • I've edited the question for clarity. Commented Aug 13, 2015 at 12:42
  • Is it possible ... shouldn't you just try it first? If you did what problems arose? Commented Aug 13, 2015 at 12:43
  • @charlietfl because we cannot "include" other files in javascript. Commented Aug 13, 2015 at 12:46

2 Answers 2

1

There is no problem with doing that.

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

Sign up to request clarification or add additional context in comments.

Comments

0

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'});

2 Comments

I've not mentioned anything about my directory structure. It exactly matches the best practices.
We can see your structure from the way everything has been declared, vlad is tottaly correct imo. the link he provided is very good aswell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.