1

As suggested I wrap all my services into a module, So, I created files to organize them:

myServicesModule.js

var myServices = angular.module('myServices', []);

serviceA.js

myServices.service('serviceA', function(){...});

and serviceB.js

myServices.service('serviceB', function(){...});

etc etc.

As a good practice, we should try to avoid global variables, so is there any way to avoid declaring a global var myServices by something I guess module resolving by name? I understand by putting all services into a single file could solve this issue, but I don't want to create a huge file.

5
  • 1
    angular.module('myServices') , without the second parameter retrieves existing module myServices Commented Jan 2, 2014 at 8:46
  • It is not encouraged to create modules for the service, controller, and directive layer. You will always have to load all modules anyways then. Split along the lines of functionality instead and put the controllers and services for a specific functionality into a module. Commented Jan 2, 2014 at 8:56
  • hi @flup, I got the suggestion from angularjs web site: Recommended Setup section from docs.angularjs.org/guide/module. what's your suggestion why not doing this? Commented Jan 2, 2014 at 9:29
  • This Best Practices YouTube video at 34:19: "Group by functionality / feature, not by type." Commented Jan 2, 2014 at 17:55
  • @flup thanks for your information, I will watch through the video. Interesting that the video and the angularjs website have different best practices ;-) Commented Jan 3, 2014 at 5:24

3 Answers 3

2

Second parameter of module function (dependencies array) is optional :

If you set the second parameter, it will create the module (and override any existing module with the same name) :

// Create the module
angular.module('foo', []).controller('FooCtrl', function($scope) {
    // ...
});

If you do not set the second parameter, it will retrieve the module by its name and fail if module does not exist :

// Retrieve module by its name and fail if module does not exist.
angular.module('foo').controller('FooCtrl', function($scope) {
    // ...
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you, I did not expect the answer is so simple!
1

The angular.module is a global place for creating, registering and retrieving Angular modules. All modules (angular core or 3rd party) that should be available to an application must be registered using this mechanism.

When passed two or more arguments, a new module is created. If passed only one argument, an existing module (the name passed as the first argument to module) is retrieved.

So this will work:

angular.module('myServices', []);

angular.module('myServices').service('serviceA', function(){...});

angular.module('myServices').service('serviceB', function(){...});

Comments

1

You don't need create variable for your service. It's possible tot do it that way`(in different files): For example, file1.js:

angular.module('myServices', []);

file2.js

angular.module('myServices').service('serviceA', function(){...});

1 Comment

Thanks you, I did not expect the answer is so simple!

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.