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

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 …

Top UK Slot Providers Worth Playing
6 October, 2026

Top UK Slot Providers Worth Playing Slot choice in the United Kingdom is vast, but the engines behind the reels matter far more than the theme on the front. A handful of studios dominate the market, and understanding how they …

Cómo identificar un casino online regulado en España
4 October, 2026

Cómo identificar un casino online regulado en España El mercado del juego online en España mueve más de 1.200 millones de euros anuales en ingresos brutos, según los últimos datos consolidados del regulador. Esa cifra explica por qué conviven tantos …

Leave A Reply Cancel reply

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

CREDO SYSTEMZ – VELACHERY

New # 30, Old # 16A, Third Main Road,
Rajalakshmi Nagar, Velachery,
(Opp. to Murugan Kalyana Mandapam),
Chennai - 600 042.
Mobile: +91 9884412301
Email: info@credosystemz.com
Web: www.credosystemz.com

CREDO SYSTEMZ – OMR

Plot No.8, Vinayaga Avenue,
Rajiv Gandhi Salai, (OMR),
Okkiampettai,
Landmark - Okkiyampet Bus Stop,
Next to Buhari Hotel,
Chennai – 600 097.
Mobile: +91 9600112302

TRENDING COURSES

  • Python Training in Chennai
  • Data Science Training in Chennai
  • AWS Training in Chennai
  • Big Data Hadoop Training in Chennai
  • Machine Learning Training in Chennai
  • Angular Training in Chennai
  • Mean Stack Training in Chennai
  • RPA Training in Chennai
  • DevOps Training in Chennai
  • R Programming Training in Chennai
  • Microsoft Azure Training

Copyright 2022 Angularjstraininginchennai.in | All Rights Reserved