Tuesday, June 16, 2015

Advantages of AngularJS Services

AngularJS Services are re-usable unit for code/functions which can use used across the application, the following are the advantages of using services.

1. Re-usability – services once defined can be used in any controller / view, thus re-usability of code.

2. Modular Programming – Services represent a specific unit of code / functionality, hence they act as building blocks for the controllers, making the application more modular. By placing the business logic in service, the controllers can be made light, the controllers just call the service functions, this improves maintainability of the applications since any changes to the service unit has to be done in only one place, in the  service code.

3. Testability – Services represent a specific unit of code / functionality which are invoked from the controllers, hence services can be easily unit tested. The service by itself is a full unit, it has input parameters and expected output, and hence it can be easily unit tested. Later the appropriate services can be called in controllers to integrate with the view.

4. Performance – Services are singleton objects / functions hence they are initialized once and used throughout the application, while controllers are loaded and cleared every time they are needed. For example in an Application which has routing configured different controllers are loaded and cleared based on the Route option, on doing so the objects in the controller will be loaded and cleared each time the controller in invoked, by placing re-usable code in the services we can avoid loading and clearing these objects along with the controller. It is recommended to place the business logic code and service side Ajax calls in the service and call the appropriate services from the controller.


Search Flipkart Products:
Flipkart.com

No comments: