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

AngularJS Introduction
20 March, 2019

Here is the Introduction of the Powerful and Super heroic Framework – AngularJS. You can find the following details here. Introduction of Angularjs. Why AngularJS ? Features of AngularJS. Reason to Learn AngularJS. Disadvantages of AngularJS. To know about the …

5 Main pillars of AngularJS
20 March, 2019

Here is the 5 Main pillars of AngulaJS. These are very important components to develop a powerful and Rich web applications in AngularJS. Lets Take a glance about the 5 Pillars of AngularJS. Directives Controllers Scopes Services Dependency Injection Please …

Top 10 Directives in AngularJS
20 March, 2019

Directives are the normal HTML Syntax. AngularJS allows you to extend existing HTML syntax with new attributes called as Directives. AngularJS comes with set of inbuilt Directives and all the Directives has individual behaviors. All the Directives are starts with …

Leave A Reply Cancel reply

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

Customer reviews across the Internet

CREDO SYSTEMZ5 Star Rating: Recommended - Best IT Training in Chennai
5 out of 5 based on 16224 reviews

Sulekha        5 Star Rating: Recommended - Best IT Training in Chennai

Yet5               5 Star Rating: Recommended - Best IT Training in Chennai

Facebook      5 Star Rating: Recommended - Best IT Training in Chennai

Google+        5 Star Rating: Recommended - Best IT Training in Chennai

Justdial         5 Star Rating: Recommended - Best IT Training in Chennai

Thinkvidya   5 Star Rating: Recommended - Best IT Training in Chennai

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