Following are some important 30 built-in services are available in AngularJS.
Why use Services?
Several services use the DOM objects, but it would have some limitations in your AngularJS application.
For ex, $location service use the objects that are already in the DOM objects (like window.location). AngularJS controls your application and it handles the changes and events properly.
AngularJS prefers, Use $location service instead of window.location DOM object.
Create an Own Service in AngularJS:
To create an own service, connect a service into the module.
Syntax for create an own service in AngularJS:
Sample Coding for create an own service in AngularJS:
Code Explanation for create an own service in AngularJS:
The ng-app specifies the root element (“myApp”) to define AngularJS application.
The ng-controller control the data of “serviceCtrl” in AngularJS application.
Here create an own services (app.service) and the service name is given as “multiply” then the service is connect with the module.
The Function (myFunc) is return the multiplication of two values and get the values from the Scope object ($scope.mul).
The “multiply” service is added with the controller function.
$scope.mul is used to declare the multiplication values 10 and 50 and the output will be updated in the <script> tag.
Sample Output for create an own service in AngularJS:
The output displays the multiplication of two numbers is 500.
It is used to design a date for specified format The $animate service exposes a series of DOM utility methods that support for animation hooks. The default behavior is the application of DOM operations. When an animation is detected or enabled $animate will do the heavy lifting to ensure that animation runs with the triggered DOM operation.
It is used to constructs Cache objects and gives access to them.
$compile
This service Compiles an HTML string or DOM into a template and produces a template function, which can be used to link scope and the template together.
Several uncaught exception in angular expressions is delegated to the service. The default implementation is represented by $log.error which logs it into the browser console.
It is used to specify the Angular’s wrapper for window.setInterval and the fn function is executed every delay milliseconds
$jsonpCallbacks
This service handles the lifecycle of callback and the JSONP requests. Override this service to customize the callbacks and they are stored in requested url.
$locale
It provides localization rules for various Angular components.
The $location service parses the URL in the browser address bar and makes the URL available to your application. Changes to the URL in the address bar are reflected into $location service and changes to $location are reflected into the browser address bar.
Each application has a single root scope. All other scopes are descendant of the root scope. Normally, scopes provide separation between the model and the view. It is delivered the emission/broadcast and subscription facility.
$sce
This service provides Strict Contextual Excaping services to AngularJS.
$sceDelegate
It is used by the $sce service and it is provide Strict Contextual Escaping(SCE) services to AngularJS.
It is loaded in the template cache for quick retrieval and it is also directly loaded into the cache in a script tag or by consuming the $templateCache service directly.
The $templateRequest service runs security checks then downloads the provided template using$http it can stores the contents inside of $templateCache. If the HTTP request fails or the response data of the HTTP request is empty, $compile error will be thrown .Note that the contents of $templateCache are trusted, so the call to $sce.getTrustedUrl (tpl) is omitted when tpl is of type string and $templateCache has the matching entry.
It is used to specify the Angular wrapper for window.setTimeout. The function is wrapped into a try/catch block and delegates any exceptions to $exceptionHandler service
It is reference to the brower’s window object and the window is globally available in JavaScript, it causes the testablility problems, because it is a global variable. In angular we always refer to the $window service, so it may be overridden, removed or mocked for testing.
angularjs service example httpangularjs service vs factoryangular services vs factoryfactory in angularjsangularjs services best practicesangularjs custom service exampleangular built in servicesangularjs factory $http examplefactory in angularjs w3schoolsangularjs service vs factory singletonangularjs when to use service vs factorywhat is provider in angularjsfactory angularjs exampleservice vs factory angularangularjs factoryangular factory constructorangularjs tutorials