Services are objects that can be used to store and share data across the application. AngularJS offers many built-in services, and each of them is responsible for a specific task. They are always used with the prefix $ symbol.
Some of the important services used in any AngularJS application are as follows:
- $http- It is used to make an Ajax call to get the server data.
- $window- It provides a reference to a DOM object.
- $Location- It provides a reference to the browser location.
- $timeout- It provides a reference to the window.set timeout function.
- $Log- It is used for logging.
- $sanitize- It is used to avoid script injections and display raw HTML in the page.
- $Rootscope- It is used for scope hierarchy manipulation.
- $Route- It is used to display browser-based path in browser’s URL.
- $Filter- It is used for providing filter access.
- $resource- It is used to work with Restful API.
- $document- It is used to access the window.Document object.
- $exceptionHandler- It is used for handling exceptions.
- $q- It provides a promise object.
- $cookies- It is used for reading, writing, and deleting the browser’s cookies.
- $parse- It is used to convert an AngularJS expression into a function.
- $cacheFactory– It is used to evaluate the specified expression when the user changes the input.
- In AngularJS, services are objects or functions that provide specific functionality and can be used across different parts of an application. They are typically singleton objects, meaning there is only one instance of each service in an AngularJS application. Services are a crucial part of AngularJS architecture and are often used for tasks such as data retrieval, sharing data between components, and encapsulating business logic.AngularJS provides several built-in services, and developers can also create custom services. Some of the commonly used built-in services in AngularJS include:
- $http: Used for making HTTP requests to fetch data from a server.
- $scope: A special object that serves as a context for expressions. It is used to bind data between the controller and the view.
- $location: Helps in reading and manipulating the browser’s URL. It is useful for navigation within the application.
- $routeParams: Extracts parameters from the URL when using routing.
- $timeout and $interval: Used for executing functions after a specified delay or at regular intervals.
- $q: Provides a framework for promises, helping manage asynchronous operations.
- $filter: Allows the filtering of arrays or objects in AngularJS expressions.
- $log: A simple service for logging messages.
- $cacheFactory: Creates and manages a cache object.
These are just a few examples, and AngularJS has more built-in services that cater to various needs within a web application. Additionally, developers can create custom services using the
service
orfactory
methods to encapsulate and share functionality across different parts of the application