user3568783
user3568783

Reputation:

Can I divide up my app.js file into multiple files in Angular JS?

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?

Upvotes: 0

Views: 470

Answers (2)

Michal Charemza
Michal Charemza

Reputation: 27062

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.

Upvotes: 1

SnareHanger
SnareHanger

Reputation: 928

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.

Upvotes: 1

Related Questions