Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

5.2 Creating an Angular Service

Similar to how we created a simple data access layer to keep all the database functionality in one place, we're going to create an Angular service to make connecting to the Express API easy. We'll use this service any time we need to talk from the front-end app to the back-end.

1.Introduction
2 lessons, 07:21

1.1
Introduction
01:02

1.2
Prerequisites
06:19

2.Getting Started
3 lessons, 30:48

2.1
Creating the App Structure
11:46

2.2
Creating the Server-Side Entry Point
10:14

2.3
Starting the Angular and Express Apps
08:48

3.Setting Up the Mongo Database
4 lessons, 27:53

3.1
Getting MongoDB Up and Running
06:08

3.2
Connecting to MongoDB
06:47

3.3
Creating the Database Schema
07:49

3.4
Creating a Simple Data Access Layer
07:09

4.Creating an API With Express
6 lessons, 29:16

4.1
Handling Requests in Express
09:57

4.2
Taking Advantage of the Express Router
05:52

4.3
Adding the `GET` Handler to the API
05:34

4.4
Adding the `POST` Handler to the API
03:18

4.5
Adding the `PUT` Handler to the API
02:17

4.6
Adding the `DELETE` Handler to the API
02:18

5.Building the Front-End Angular App
6 lessons, 45:52

5.1
Creating the Front-End Models
06:57

5.2
Creating an Angular Service
07:31

5.3
Making HTTP Requests From the Service
08:33

5.4
Setting Up the User Interface
09:05

5.5
Creating All the Components
05:28

5.6
Adding Routing to the App
08:18

6.Creating the App Components
12 lessons, 1:00:02

6.1
Adding the View Lawn Markup
05:55

6.2
Adding the View Lawn Code
06:51

6.3
Adding the Add Lawn Markup
04:34

6.4
Adding the Add Lawn Code
07:41

6.5
Adding the Edit Lawn Markup
03:06

6.6
Adding the Edit Lawn Code
04:11

6.7
Adding the View Application Markup
02:54

6.8
Adding the View Application Code
07:46

6.9
Adding the Add Application Markup
02:16

6.10
Adding the Add Application Code
04:49

6.11
Adding the Edit Application Markup
04:20

6.12
Adding the Edit Application Code
05:39

7.Conclusion
1 lesson, 03:18

7.1
Conclusion
03:18


5.2 Creating an Angular Service

Now that we have our models on the back-end exposed via Express and our models on the front-end in the perspective of Angular, it's time to start to glue those things together. And typically, when you're creating Angular-based applications, you're gonna do that via a service. Now this service is gonna be in charge of living in the Angular world on the front-end and then making HTTP requests to the back-end, or to some other application or server or service somewhere, to interact with data. And it just so happens we have one of those. So now we need to create the service. Now, there's a number of different ways you can do this, you can create it by hand. But one of the ways that I like to do it is by using the Angular CLI. We went through the trouble of installing that earlier, why don't we continue to use it throughout the application as much as possible? So let's go ahead and switch back over to a shell in our terminal here. And we're gonna go ahead and create a service using the CLI. So the way that we do that is, we use ng again and then we want to generate. So you can either type out generate or just use the shortcut g. And we wanna generate a service, so we could type out service or we could just type s, so we wanna generate a service. And now we're gonna give it a name. Now, you can just give it a name and hit Enter. And if you do that, it's going to create whatever service you asked it to in the root of your app directory. I don't really want that, I like to kinda keep things together as much as possible. So if I'm creating services, I wanna put them in a services folder. So I'm gonna say services/, and I'm gonna call my service lawn. Now I could call it lawn service, but what Angular is going to do, or what the CLI is going to do, is it's going to append the word service onto the end of whatever you type in here. So if I type in lawn service, it's going to create the lawn service service. So if you wanna have service on the end of it like I do, then you should just leave it off the process we're using right now to create it via the CLI. So I'll go ahead and click Enter, and now it's gonna go ahead and create two files. It's gonna create the lawn.service file, the actual code file as well as a spec file for tests. So let's go in here and take a look, we now have a services folder, and I have my lawn.service.ts. And here is the basic structure or skeleton of a simple service in Angular. So we're gonna go ahead and put a little bit in here and get it kind of set up. We're not gonna do the implementation, we're gonna save that for the next lesson. But there's a little bit of wiring up you have to do when it comes to services as opposed to when you're creating components, which we'll do a little bit later on. So the first thing that we're gonna want to do is we're going to want to talk about what it is we're gonna use to communicate with the backend. And like I said before, we wanna make HTTP requests. And it just so happens that there is something already like that built into Angular that we can take advantage of. So the way we're gonna do this is we are gonna use a little bit of dependency injection, which Angular supports. And we're gonna create in the constructor a private instance of an httpClient and that's gonna of type HttpClient. Now right out of the gate, you're gonna see that's it not included, we're gonna get little squiggly lines here. But remember, if you running on a Mac you can use Cmd+., or on Windows you can use Ctrl+., and I'm gonna do that right now. And you're gonna see here, more than likely it's gonna say, yeah, we can Import HttpClient from module "selenium-webdriver/http". That is not the one that we want. So, really, the one that we want lives in a subfolder of Angular. So we're gonna do this one by hand. We're gonna use import, we're going to import HttpClient. And we're gonna import that from @angular/common/http, like that. And it would help if I were to type it in correctly. So this is the version of the HttpClient that we want. This is gonna make our development very, very easy. And just as a quick note. If you did declare a type like this in the constructor of one your services, or honestly, any of your classes, and you declare it as private, automatically, you're going to get that as a private instance variable tied to your class. So now I'm gonna have a this.HttpClient anywhere in my lawn.service, so that's kind of a nice little thing to have. So we've brought in HttpClient, that's good. But we're also gonna wanna bring in the actual models that we're gonna be dealing with, so we're gonna wanna bring in lawn. So I'm gonna import lawn that we created earlier. I'm gonna bring that in from, and this is back a level in models/Lawn. So that's gonna be the Lawn model that we created on the Angular side that's gonna map into what we created on the backend. And you're gonna see why we're gonna do that in an upcoming lesson. Now, another thing that we're gonna wanna do once we've done this is, obviously, fill out the implementation. But we're not quite ready for that just yet, so I'm gonna save what I've done here so far. Now you're gonna say, okay, well, there's nothing really here, what else would we have to do? Well, what we have to do when it comes to building services in Angular is we kind of have to register them as part of our application. And the way that we do that is through our app.module. Now, this is where you start to declare and define everything that you're gonna be using throughout your application.. It can be a little bit overwhelming once your application gets larger. And it can be a little overwhelming but it's really not all that bad. So the way to think about this, at least the way that it kind of helps me a little bit, and if you say it enough, you'll start to remember it. But we have four sections down here to start with. But there's three that are important to us for this particular application, declarations, imports, and providers. So if you just remember that you declare components, you import modules, and you provide services. Now, I know that if you've never heard that before it might sound a little strange. But the more you say that, the more you'll get used to it and the more it'll become a little bit normal to you. So you declare components, you import modules, and you provide services. So what we're gonna do now is we're going to come down into our imports and we need to add an import. So in order for us to be able to use that HttpClient that we were using in the service, we have to be able to bring in an HttpClientModule. So we're gonna bring that in and as you see, that's going to help us to facilitate using that HttpClient later on. And when I added that in here and selected that one, you're gonna see that it added this nice little import statement for me, right here. And make sure that it's the correct one right here, the same location as that HttpClient in @angular/common/http. All right, so we need to import that, and now that we have a LawnService, we wanna be able to provide that as well, so we're going to provide our LawnService. And when I do that, we're going to get another import statement added in for us right here, into ./services/lawn.service. So let's go ahead and save that. So now we have created our basic skeleton of our service, which we're gonna augment in the next lesson. And we have also made sure to register that service and its dependencies in here in our app.module by bringing in the HttpClientModule. We're importing our module and we're providing our LawnService. So now that we have that set up, we can now start to create the actual implementation of our service, which we're gonna do in the next lesson.

Back to the top