AngularJS Providers

  • Each web application build in composed of objects. These objects need to be instantiated and wired together for the app. Angular apps most of these objects are instantiated and wired together automatically by injector service.
  • A provider is actually a configurable factory. It accepts an object or constructor.
  • AngularJS uses $provide to register new providers. The providers basically create new instances, but only once for each provider.
  • The $provide service has additional methods to register services without specifying provider.
  • These providers are available in $provide.
    • Value - It registers a value/object that can only be accessed by services not providers.
    • Factory - It is used to registers a service factory function that will be wrapped in a service provider object.
    • Service- It is used to registers a constructor function that will be wrapped in a service provider object. It is invoking a constructor with new operator.
    • Constant – It registers a value/object that can be accessed by providers and Services.
    • Provider – It is used to register a service provider with $injector.

Syntax for providers in AngularJS:

 module.provider(‘providername’, function);

AngularJS provider:

Name Description
$anchorScrollProvider It is used to disable automatic scrolling at any time $location.hash() changes.
$animateProvider $animate service is the default implementation that does not perform any animations instead of synchronously performs DOM updates.
$compileProvider List a new directive with the compiler.
$controllerProvider It allows controller registration through the register method. $controller service create a new controllers.
$filterProvider Filters are funtions which transform input to an output. But filters need to be an Dependency Injected.
$httpProvider It is used to change the default behavior of the $http service.
$interpolateProvider It is used for aligning the interpolation markup. By default use this curly braces {{ and }}
$locationProvider To configure exactly how the application deep linking paths are stored.
$logProvider It is used to configure in what way the application log messages.
$parseProvider To configure the default behavior of the $parse service
$qProvider This provider in module ng.
$rootScopeProvider It is the provider of the $rootScope service.
$sceDelegateProvider This provider allows developers to configure the $sceDelegate service.
$sceProvider It allows developers to configure the $sce service
$templateRequestProvider It is used to configure the options delivered to the $http service what time making a template request.

Sample Coding for providers in AngularJS:

 Tryit<!DOCTYPE html>
        <title>Wikitechy AngularJS Tutorial</title>
        <script src="
        angular.min.js"> </script>
        <div ng-app="myApp" ng-controller="providerCtrl">
            <h3>provider example in AngularJS Tutorial</h3>
             Display the name using provider:<b>{{ providervalue }}</b> 
            var app = angular.module('myApp',[]);
            app.provider('unicorn', function() { 
                this.setName = function(name) {
           = name; 
                this.$get = function() {
                    var self = this;  
                    return { 
                        getvalue: function() {   
                            return + "!!!";   
                unicornProvider.setName('welcome to AngularJS Tutorial');  
            app.controller('providerCtrl', function($scope,unicorn) { 
                $scope.providervalue = unicorn.getvalue();   

Code Explanation for providers in AngularJS:

Code Explanation for AngularJS Providers

  1. The ng-app specifies the root element (“myApp”) to define AngularJS application.
  2. The ng-controller control the data of “providerCtrl” in AngularJS application.
  3. create a provider (app.provider) and the provider name is given as “unicorn”.
  4. Here set the name function (like this.setName=function(name)) and the name parameter is passed with function.
  5. The name function value is returned by using the $get method on an instance of the function passed to unicorn provider.
  6. The provider(“unicorn”) is injected to the config function.
  7. The “providerCtrl” used to create the controller for the Application with arguments $scope object and unicorn provider.
  8. The $scope.providervalue is used to get the name from the $get() method.

Sample Output for providers in AngularJS:

Sample Output for AngularJS Providers

  1. The output displays the content of “welcome to AngularJS tutorial!!!”.

Related Searches to angularjs Providers