- Overview
- Transcript
2.7 Creating a Service With the CLI
As well as creating components, we can also use the CLI to create services. Just like in the original AngularJS, services are commonly used to share application logic and business rules between different components. In this lesson you'll learn how to create one.
1.Introduction6 lessons, 42:00
1.1Introduction00:48
1.2Get Started With Angular-CLI11:09
1.3Developing With Angular-CLI13:17
1.4TypeScript vs. JavaScript06:54
1.5Angular Modules From the CLI04:31
1.6CLI Options05:21
2.Get Started With Angular7 lessons, 42:38
2.1Bootstrapping the Application04:30
2.2The Application Module04:15
2.3The Application Component08:06
2.4Component Styling03:06
2.5Global Styling05:11
2.6Creating a Component With the CLI09:34
2.7Creating a Service With the CLI07:56
3.Core Concepts7 lessons, 55:20
3.1Component Trees06:20
3.2Dependency Injection06:52
3.3Content Projection05:38
3.4Component and Directive Lifecycle Methods06:31
3.5Component-Only Lifecycle Methods05:28
3.6Decorators07:36
3.7Models16:55
4.Template Deep Dive11 lessons, 1:10:56
4.1Basic Data Binding With Interpolation05:35
4.2Property Bindings07:07
4.3Attribute Bindings03:29
4.4Event Bindings08:16
4.5Class and Style Bindings05:44
4.6The `NgClass` and `NgStyle` Directives05:04
4.7The `*ngIf` Directive04:41
4.8The `*ngFor` Directive09:29
4.9Inputs05:33
4.10Using Pipes in a Template07:31
4.11Using Pipes in a Class08:27
5.Forms10 lessons, 1:45:41
5.1Handling User Input With Template Reference Variables07:06
5.2Template-Driven Forms11:10
5.3Template-Driven Forms: Validation and Submission14:00
5.4Reactive Forms11:26
5.5Using a `FormBuilder`08:01
5.6Reactive Validation With Built-in Validators14:53
5.7Creating Custom Validators for Template-Driven Forms12:18
5.8Creating Custom Validators for Reactive Forms08:26
5.9Observing Form State Changes12:40
5.10Working With the `@HostListener` Decorator05:41
6.Routing9 lessons, 1:15:10
6.1Defining and Configuring Routes07:53
6.2Rendering Components With Router Outlets10:14
6.3Using Router Links for Navigation05:25
6.4Navigating Routes Using the Router06:24
6.5Determining the Active Route Using an Activated Route07:16
6.6Working With Route Parameters10:42
6.7Using Route Guards07:36
6.8Observing Router Events10:55
6.9Adding Child Routes08:45
7.Using the HTTP Client5 lessons, 56:24
7.1Sending an HTTP Request10:52
7.2Handling an HTTP Response11:22
7.3Setting Request Headers12:33
7.4Intercepting Requests09:04
7.5Finishing the Example Application12:33
8.Testing10 lessons, 1:23:27
8.1Service Unit Test Preparation10:45
8.2Unit Testing Services13:24
8.3Component Unit Test Preparation12:35
8.4Unit Testing Components07:27
8.5Unit Testing Component Templates06:58
8.6Unit Testing Pipes04:41
8.7Unit Testing Directives04:56
8.8Unit Testing Validators04:48
8.9Unit Testing Observables11:37
8.10Unit Testing HTTP Interceptors06:16
9.Building for Production1 lesson, 03:40
9.1Building for Production03:40
10.Conclusion1 lesson, 01:32
10.1Conclusion01:32
2.7 Creating a Service With the CLI
Hi folks, in this lesson, we're gonna see how we can use the Angular CLI to scaffold out a new service. If components are the most common Angular feature, services are the next most common. Just like in Angular JS, in modern Angular services are used to share common functionality between components. We only have a couple of components in the application so far, but we will be adding some more shortly. And we are going to want to share functionality between them. So let's create a service ready to do that. At this stage, we can create a very simple service to store some constants in. Just like with components, we can use the CLI to quickly and easily generate a new service for us using a very similar command that we used to generate the component in the last lesson. This command is very similar to the one we used before, we are running the ng generate command. This time we want to generate a service and that service is gonna be called constants. So when this command has finished, we can see that it has created the service and the service.spec file for us. It hasn't updated the app module, because we might not want this to be a global service that can be injected into any component. We can restrict services so that they only are available within a particular component, but in this case, we are going to use it throughout the app. So, let's take a look at the service that has been generated. And we can see that it has just been created in the root of the app folder, and this is much simpler than the component. We import something called Injectable from Angular. This is another type of decorator, but this is a much simpler decorator. We don't have to pass in a configuration object. We can just annotate a class to specify that we want it to be injectable. And this constant service will be injectable. We'll be injecting it into other components and we also have an empty constructor. Now services don't have life cycle methods. So the on init interface hasn't been imported, and the class hasn't given an ng on init method. You can't do this with services, that's why the CLI hasn't done it for us. So we don't actually want this service to be generated in the root of the application. So we're going to delete it and create it again. The problem is that we'll generally add a number of services, it's not a huge problem at the moment because we only had one of them. But when we have lots of services and their associated specs files, the root of the folder will quickly become very cluttered. The architecture of components is fantastic. Each component is a feature of the application, and everything to do with that feature goes into the component folder. The folder becomes a feature folder. And this is great for all sorts of reasons, including things like planning out work and making estimates, and just keeping the application code nicely organized. The architecture for services is not always so clear. Because they're so often shared between components, there is often no single feature to which they belong. So we can put them in a more general centralized location. What I've seen done before is to have them in a folder in the root of the app folder called _services, and all of these shared services can go in here. Giving the folder name an underscore at the start means that the folder will go to the top of the app folder. And so, it will always be easy to find even if we got lots of other feature folders. Services don't always have to be shared across the whole application. On really big features you might break them down into a number of sub features and have a service that is only available for use with these sub features. So in that case, the service might go into the parent feature folder, but that's not the case here. We don't have anything that's gonna be that big to warrant numerous sub components. So, let's create the service again. We can run almost the same command as before, but with a slight modification. So, this time instead of specifying just the name of the service we want to create, we specify a path instead. So we'll see almost the same output as before. Let's just refresh the editor in the background here. And it has created the folder for us, but it hasn't given it an underscore, it's given it a hyphen. So this seems to be a problem with the CLI. It might get fixed at some point, it might not. Let's just fix that ourselves manually now. And we can see that our service is now in this generic _services folder. And when we want to add new services, when we specify the path _services, the CLI will be able to find this folder, and it will put them in the correct place. So that's fine. So let's go ahead and add some constants now. And we can do this in the constructor(). So you'll notice that we get some lint errors here. Let's just hover over one of them. And it says that the property COLORS does not exist on type ConstantsService. What this means is that we need to add some types to the class to specify the types of properties. We can add the type information above the constructor. So we specify that each of these properties should be public. They could also be marked as private, meaning that they can only be used within the same class, or protected, which means that they can be used in the same class and any class that extends this class. But in the case of this particular service, this wouldn't be very useful. So, the editor's still not entirely happy, and that's because the last property is not actually an array of strings, it's an array of numbers. And now we can see that Visual Studio, at least, is happy with this class declaration. So now this service is ready to be used. So in this lesson, we saw how to create a service using the CLI. We saw that services are simpler than components and do not have associated templates or style sheets. We saw that we can use the Injectable decorator to tell Angular that we intend to inject this service into other services or components. And we also saw how to add type information for properties of a service. Thanks for watching.