Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 916 of the free egghead.io lessons, plus get Angular 1.x content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



Services, Factories, and Providers: Creating a Provider

7:45 Angular 1.x lesson by

Creating your own services in AngularJS can be confusing. What are the differences between an AngularJS module's Service, Provider and Factory functions? This lesson shows how Angular's provider function is just a configurable JavaScript module.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Creating your own services in AngularJS can be confusing. What are the differences between an AngularJS module's Service, Provider and Factory functions? This lesson shows how Angular's provider function is just a configurable JavaScript module.

Avatar
Paul

Great little series on services vs factories vs providers. Thx Craig.

In reply to egghead.io

Sometimes you want a service that's configurable like the HTTP service built into Angular. The way you configure a service is to go into the DotConfig function of the module, and send in the companion provider object to your service. HTTP has an HTTP provider which can be used to configure the HTTP service.

We could do things like set caching to "true" for requests, so that all requests made throughout the application, such as this one and this controller, would get cached. That's a concrete example of how a service can have a provider that's configurable once in the application. Then that configuration affects all uses of the service inside the application.

Now that we've seen a provider, let's create one in plain JavaScript. We create a function called say, "droid-provider," and the function returns an object. The interface for this object, first of all, have a function to configure the service. The name configure here is not important to Angular. It can be named anything like, "set base URL."

I'm naming it "configure" here, for clarity. Then there is a $get function whose name is important. The get-function is what will return the actual service object itself. We'll go ahead and implement our service. We'll give it a name property and a speak function. We'll have the speak function, say, return, "Hi, I am" and then concatenate the name to that.

To use this provider function we created, it's a two-step process. First, we get a reference to the provider object itself. If I called droid-provider here, what I'll get back into the provider object is this object, which will have both a configure and a get method. Again, the configure method is to configure the object. The get method is to return the instance of the object.

I'll go ahead and call configure on our provider. We won't actually configure it yet. We'll come back and do that in a second. Then I can create a local variable, "droid," and tell the provider to get me an instance of the service object itself.

What's happening when I call get here is, it calls $get right there, which will return me an object. I can then work with the object. I can say, "droid.name = IG88." Then I can log out the droid speaking. If I hit save, I get, "Hi, I am IG88" over here in the console.

Now to make this configurable, what we'll need to do is pull out this, "Hi, I am," and introduce a variable called, "greeting." I'll make that variable private up here and initialize it to an empty string. We can then pass a settings object into our config function and set the greeting. We'll set it simply to the settings object's greeting property.

This may be confusing because I don't have a greeting property, yet. But what we can do is come down here to where we configure our provider, pass in a settings object, give it a greeting property, and then set this to something different. Let's say, "Greetings, I am." If I save now I get, "Greetings, I am IG88."

OK, now that we've seen how to create a provider object in plain JavaScript, let's take our provider object, switch over to our empty Angular application, and paste the code across. Register this provider with Angular by saying, "dot provider," off the module. We'll give it a name of, "droid," and point at our droid provider.

If we then ask for an instance of a droid and set the droid's name to IG88, and then log out to the message property on our controller, the droid speaking, and save, we should see, "IG88" here.

Now notice, we don't see, "Hi, I am," yet or any type of greeting, because we haven't configured the object. To configure our service using our provider we call the DotConfig function of the module. We ask for an instance of the droid provider. This will send us an instance of the object that's being returned right here, so we'll have a configure function on it and a get function on it.

We can go ahead and configure our droid service. We can pass in a settings object, give that a greeting property, set that equal to, "Greetings, I am," and then save. We get, "Greetings, I am IG88," because we've configured our object using a provider.

It's important to understand that this droid provider here being passed in, is not this function. It's the result of this function being invoked, which returns us this object literal here, which then has a configure function and $get function.

This name of droid provider is by convention. When we register the provider with the name "droid," we get two objects. We get the service itself named "droid," and we get droid with the word "provider" appended, which represents this first outer object that's being returned.

To recap, if you want a service that's configurable, you'll need to return an object that has two functions. One to configure the object and one to get the object. The $get function name is important here, because Angular needs to know to call that one. You'll be calling this function, so it's not as important what you name it.

To register this provider object with Angular, you call dot provider and pass it that function. Angular then knows that when you ask for this object plus the word "provider" to return this outer object, which will give you both a configure and a get.

You can then call the configure function and configure your service. Then, when you ask for your service as we did down here in the controller, Angular will use this provider object and call the get method on it. Which will return you an instance of an object as your service, and that will be dependency injected where you ask for it.


Featured course:

Create an SVG Icon System

Create an SVG Icon System

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?