Services are one of the most important concepts in AngularJS. In general services are functions that are responsible for specific tasks in an application. AngularJS services are designed based on two principles.
- Lazily instantiated
Angular only instantiates a service when an application component depends on it using dependency injection for making the Angular codes robust and less error prone.
- Singletons
Each component is dependent on a service that gets a reference to the single instance generated by the service factory.
AngularJS provides many built in services, for example, $http, $route, $window, $location and so on. Each service is responsible for a specific task, for example, $http is used to make an Ajax call to get the server data. $route defines the routing information and so on. Builtin services are always prefixed with the $ symbol.
AngularJS internal services
AngularJS internally provides many services that we can use in our application. $http is one example. There are other useful services, such as $route, $window, $location and so on. Some of the commonly used services in any AngularJS applications are listed below.
- $window Provide a reference to a DOM object.
- $Location Provides reference to the browser location.
- $timeout Provides a reference to window.settimeout function.
- $Log Used for logging.
- $sanitize Used to avoid script injections and display raw HTML in page.
- $Rootscope Used for scope hierarchy manipulation.
- $Route Used to display browser based path in browser URL.
- $Filter Used for providing filter access.
- $resource Used to work with Restful API.
- $document Used to access the window. Document object.
- $exceptionHandler Used for handling exceptions.
- $q: Provides a promise object.