Understanding AngularJS services
When you are building medium to large-scale applications where certain functionalities are common across different pages or sections, then instead of repeating the same piece of code within every controller, it's best to write it within a service and call it within the different components.
A classic example would be getting the list of products from a backend web service. The code to make the web service call, passing in the authentication tokens or API keys, getting the response back, and parsing the response can all be put into a service. This service can then be called from the various controllers, directives, or other components that need to display the product list.
A couple of points to remember with regard to services are as follows:
They are singleton objects that are initiated only once, and they persist throughout the lifetime of the app
Services are lazy loaded, that is, they get initiated only when an application component depends on it
These services...