- Overview
- Transcript
3.4 Component and Directive Lifecycle Methods
Components and directives have a well-defined lifecycle, with Angular creating and destroying each of them as required as users interact with our app. Angular provides useful hooks into key moments during the lifecycle of a component or directive, and we'll focus on these methods in this lesson.
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
3.4 Component and Directive Lifecycle Methods
Hi folks, in this lesson we're going to learn about the lifecycle methods that components and directives have in Angular. Angular provides a series of different hookes that we can use to react to key moments in a component's life cycle. Let's take a look. One of the most common lifecycle methods is ngOnInit. This lifecycle method is so common that the Angular CLI adds it to all components that we generate by default. We did take a brief look at this earlier in the course but let's just go over it again. So we can open up any of the components that we've created and see it. Let's just open the Controls Component class file. So we import OnInits from the Angular core module, and we specify there are component implements OnInits within the class. We then have to add a method called ngOnInit. In Angular we'll invoke this method once it has initialized the component. So we can use this method to do any setup that is required. Now all classes also have a constructor. And all components can make use of the ngOnInit method. So, what should we use each one of these things for? For components or directives specifically, we should use the constructor only for dependency injection. Any actual initialization work that needs to be carried out should happen on the ngOnInit method. Because the constructor will be invoked automatically when Angular news up the component class deep inside Angular somewhere, way before the page loads. We can't test any code inside the constructive area easily. The ngOnInit method on the other hand can be tested very easily. Because we can simply code the method ourselves from within the test. So it's more maintainable if we have a simple rule like always do initialization in the same place. So the ngOnInit method is invoked when the component is initialized. This means that any values parsed into the component has been set. And as I mentioned, the instructor will have already been called way before the ngOnInit is invoked. In terms of the order in which life cycle methods are invoked, ngOnInit may be the first or second life cycle method to be invoked. If we do pass data into the component, another life cycle method will be invoked first. The ng on-changes method. But if we don't pass any data into the component, like with the controls component at the moment, we aren't passing any data into the component here. And so the ngOnInit method will be the first to fire. When we are passing data into components, the ng on-changes method will be invoked first as it receives the initial data. And then the ngOnInit method will be called after this. The ng onChanges method may, however, may be called subsequent times whenever the data it is consuming changes. The ngOnInit method isn't passed any arguments. But the ngonChanges method does receive an object containing the old and new values for the data that changed. If we want to make use of the ngOnChanges method, we just need to import, it, implement it and add the method. So we import two new things here on changes, which is an interface and simple change that would be simple changes. Which is a class, both of these are imported from the Angular core module.. So now we can implement on-changes. We just do that by providing it after the OnInit, we don't need to specify implements twice. And now we need to actually specify the ngOnChanges method. So the ngOnChanges method will receive the changes, these will be of the type simple changes. Another lifecycle method is ngDoCheck, which we can use to detect changes that may be outside of Angular's control, like if we're using a third party library or something. As with the other life cycle methods, we can import the interface, add it to the implements list and add the method for it. This method will be called after ngOnChanges and after ngOnInit and this method doesn't receive any arguments. This method will be invoked every time the Angulars own change detection runs. So this method could both be invoked numerous times in a components lifecycle. The last general lifecycle method that is available for both components and directives is ngOnDestroy. We should use this method to tidy up after a component, and unsubscribe from observables or remove any event handlers in order to avoid memory leaks. This method is called just before Angular, destroys the component. And again this method doesn't receive any arguments. So we don't need to actually use any of these lifecycle methods in this components. So let's get rid of everything that we've added in this lesson, including the ngOnInit method, which the CLI added for us, but which we aren't going to use with this component. So throughout the course, we will be using some of these lifecycle methods and at that point we can looking to how they are used in a bit more depth. I just wanted to show you how they can actually be implemented and the order in which they are usually fired. We're not in a place yet, really, where we can make use of any of them. So, in this lesson, we've looked at the lifecycle methods the Angular will invoke for us if we define them at different point of the lifecycle of our component or directive. We haven't looked at any directives yet, we will come to those later in the course. We saw that there are four lifecycle methods that can be used with both components and directives ngOnChanges, ngOnInit, ngCoCheck and ngOnDestroy, which are invoked on a minimum in its respective order. Some of them, like ngOnChanges, or ngDoCheck, might be evoked numerous times, depending on how the component is interacted with. Thanks for watching.