0

In my app.js file I have my Angular code like this:

var app = angular
    .module('app',
    ['...'])

    .config([
        '...',
        function (
            ...) {

        }])

    .factory('requestInterceptor', function ($q, $rootScope) {
        ..

        return {
            ..
        };
    })

Is it possible for me to move the code for the config and the factory out of the file and if so then how do I link this up to the app variable?

2
  • sure, new file => app.factory( .... Commented May 2, 2014 at 13:39
  • You should take a look at Yeoman Generator-Angular Commented May 2, 2014 at 13:45

2 Answers 2

1

I have an app setup this way.

What you can do is declare the module in one file before and separate from everything else. Then just make sure it's the first script file referenced in your HTML.

After that you should be able to just add factories, configs and what not to the app in other files.

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

1 Comment

Can you give me an example of this. Just with a few lines in each file so I can see how it all fits together.
1

Yes. If you define a module, app:

angular.module('app',[]); // Including the [], or list of module dependencies

then you can refer to it later, in other files, by calling module without the second parameter:

angular.module('app').factory('requestInterceptor',...

It is confusing: module is used for both creating a new module, and to add to an existing one, depending on whether you have passed an array as its second parameter.

Comments