How do you make a service configurable in angularjs?

angularjs, angularjs-service

Solution

In order to create a configurable service, you need to use a `provider`, see: http://docs.angularjs.org/guide/providers. Providers allow you to configure the factory or service in the module's config block before it is instantiated. For a simple example, see http://plnkr.co/edit/QMLBBQ3obE90FtCA2eBu

For a more complete/complex example of how to use configurable services, I would suggest looking at the Restangular project, which also demonstrates a method of providing default values that can be overriden by the app developer.

Problem

I got services in angular that I need to be configurable, this way I can remove coupling from my app and share some of my code. My question is how do you achieve this ? With module.value() and dependancy injection ? Then how do you get a default value ? How do you make a value calculated at init and not hardcoded ? With a another service ? How do you make that generic ? Do you have a code exemple, because I'm a bit lost with this.

Original source

Related problems