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 ...

Get AngularJS Web Application Development Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.