Call Us Now!
Velachery: +91 9884412301 | OMR: +91 9600112302
info@credosystemz.com
AngularJS Training in ChennaiAngularJS Training in Chennai
  • Home
  • Course Content
  • Realtime Projects
  • Interview Q&A
  • Contact Us
  • Home
  • Course Content
  • Realtime Projects
  • Interview Q&A
  • Contact Us

Blog

  • Home
  • Blog
  • Understanding Providers in AngularJS

Understanding Providers in AngularJS

  • Posted by admin
  • Categories Blog
  • Date March 20, 2019
  • Comments 0 comment

In our previous Article, we discussed about service and Factory. Here is the complete details about Providers.

Provider allows us to create and inject a service into configuration phase of our application. For example, If we want to set API key to access the application, we can set the API key via provider and pass the provider to the application while its in config phase.

 

Creating Provider service using $provider service in module.config

Module.config(function ($provide) {

$provide.provider(‘globalSetting, function() {

this.$get = function {

var appname = “My 1st Application”;

return {

appName: appname

};

}

})

});

 

Explanation:

  1. Inject $provide built-in service in config method.
  2. Create a provider service by using $provide.
  3. The Provider service function has 2 parameters
    1. The provider service Name.
    2. Function
  4. The provider must have this.$get function
  5. Return the object literal from this function

 

Now the “globalSetting” provider is available for all other components. Because it has some properties and methods in a object.

 

How to use Provider in Controller ?

app.controller(“myCtrl”, function ($scope, globalsetting) {

$scope.name = globalsetting.appName;

});

 

Now we can use the “appName” in our View,

<div ng-controller=”myCtrl”>

{{name}} // it will bind  My 1st Application

</div>

 

Create a Provider service by using module.

Module.provider(‘globalsetting’, function () {

var appname = “LAWYER APP”;

this.setAppName = function (value) {

appname = value;

}

this.$get = function () {

return {

appName: appname

};

}

});

 

Explanation:

1.Creating provider function through our app Module.

2. It has 2 parameter

i. Provider Name

ii. Function

3. By default we set appname = “LAWYER APP”

4. Here we set a function for change the app via function parameter.

If someone calls this function then the app name will be replace.

5. Return the object literal from this.$get function.

So we set a provider function here.

 

How to Inject Custom Provider into .config method and controller ?

 

Inject provider into .config Method

When we going to inject our custom provider into .config method, we must add “Provider” in suffix of our Custom Provider Name.

Our custom provider name is ‘globalsetting’ so we should inject as ‘globalsettingProvider’

 

Example:

Module.config(function (globalsettingProvider) {

globalsettingProvider.setAppName(“Great App”);

});

Now the appname Replaced to “Great App” . We can retrieve the appname into any component.

 

Inject provider into Controller

Module.controller(‘myCtrl’, function($scope, globalsetting) {

$scope.name = globalsetting.appName;

});

To Know more about the complete collection of AngularJS Topic, Please visit Complete Updated Angularjs Topics

  • Share:
admin

Previous post

Routing in AngularJS
March 20, 2019

Next post

What is Services and Factory in AngularJS ?
March 20, 2019

You may also like

Что можно фотографировать в повседневной урбанистической жизни
9 November, 2026

Что можно фотографировать в повседневной урбанистической жизни Городская пространство дарит изобилие оптических перспектив для фотографа, способного видеть окружающие детали. Архитектурные структуры, дворовые эпизоды, игра света на стенах и повседневные 7к казино эпизоды бытия горожан порождают многообразный основу для съёмки. Фотография …

Casino on-line entry overseas: currencies, languages and local limitations
6 November, 2026

Casino on-line entry overseas: currencies, languages and local limitations Online wagering platforms run across multiple regions, yet availability requirements change substantially dependent on player position. Providers must adhere with local regulations, which generates a complicated framework of availability. Geographic restrictions …

Casino on-line guide for modern players
6 November, 2026

Casino on-line guide for modern players Digital gambling platforms offer entertainment for players worldwide. Modern operators provide thousands of games, secure payments, and promotional campaigns. Understanding platform functions helps players select Codice bonus Big Casino a suitable operator. This guide …

Leave A Reply Cancel reply

Your email address will not be published. Required fields are marked *

non gamstop casinos

non gamstop casinos

casinos not on gamstop

casinos not on gamstop

casinos not on gamstop

casinos not on gamstop

non gamstop casino

non gamstop casinos

non gamstop casinos uk

best non gamstop casinos

Copyright 2022 Angularjstraininginchennai.in | All Rights Reserved