AfterViewInit, AfterViewChecked, AfterContentInit & AfterContentChecked In Angular
The life cycle hooks are AfterViewInit, AfterContentInit, AfterViewChecked, and AfterContentChecked. Angular raises them during a Component's lifecycle. In this article, we will discover what they are and when Angular uses them. We also discover the distinctions between AfterViewInit, AfterContentInit, AfterViewChecked, and AfterContentChecked.
Lifecycle hooks
When angular instantiates a component (or directive), the component's life begins.
Invoking the component's constructor and injecting the services via dependency injection are the first steps in the instantiation process.
When Angular creates a component, it initiates the component's change detection cycle. It initializes the component and checks and updates any data-bound input properties. The following life cycle hooks are then raised.
If Angular detects any changes to the Input property, it will fire the Onchanges event. It is called whenever angular detects a change in the input.
OnInit indicates that the component is ready to use. This hook allows us to run any initialization logic, change a few properties, and so on. This hook only executes once.
Because change detection may fail, we can use DoCheck to execute bespoke change detection.
Content Vs View
Before we go into these hooks, it's important to understand the difference between Content and View. AfterContentInit and AfterContentChecked are hooks that deal with content, while AfterViewInit and AfterViewChecked are hooks that deal with views.
Content
The external content injected into this component via Content Projection is referred to as content.
The HTML content is passed from the parent component to the child component using content projection. The template will be shown in a specific location by the child component. The ng-content element is used to create a spot in the child component's template, as seen below.
<h2>Child Component</h2> <ng-content></ng-content> <!-- place hodler for content from parent -->
The content is injected between the opening and closing elements by Parent. This content is passed to the child component by Angular.
<h1>Parent Component</h1> <app-child> This <b>content</b> is injected from parent</app-child>
View
AfterContentInit
AfterContentChecked
AfterViewInit
AfterViewChecked
Init Vs Checked
Init Hooks
Checked Hooks
import { Component } from "@angular/core"; @Component({ selector: "app-child", template: ` <div style="border:solid; border-width:1px;"> <h2>Child Component</h2> message : <input [(ngModel)]="message"> <p> Injected Content Below</p> <ng-content></ng-content> </div> `, }) export class ChildComponent { message = "" ngOnChanges() { console.log(' ChildComponent==>ngOnChanges'); } ngOnInit() { console.log(' ChildComponent==>ngOnInit'); } ngDoCheck() { console.log(' ChildComponent==>ngDoCheck'); } ngAfterContentInit() { console.log(' ChildComponent==>ngAfterContentInit'); } ngAfterContentChecked() { console.log(' ChildComponent==>ngAfterContentChecked'); } ngAfterViewInit() { console.log(' ChildComponent==>AfterViewInit'); } ngAfterViewChecked() { console.log(' ChildComponent==>AfterViewChecked'); } }
- Using ngModel, we have an input FORM element that is connected to the component's message attribute.
- <ng-content></ng-content> serves as a placeholder for the parent's injected content.
- for example whenever change detection invokes the hook, the component logs to console.Console.log(' ChildComponent==>ngOnChanges');
import { Component, ViewChild } from "@angular/core"; import { ChildComponent } from "./child-component"; @Component({ selector: "my-app", template: ` AfterConentInit, AfterContentChecked, AfterViewInit, AfterViewChecked <app-child> <b>Injected</b> content from the <i>Parent</i> </app-child> `, }) export class AppComponent { message=""; @ViewChild(ChildComponent) viewChild: ChildComponent; ngOnChanges() { console.log('AppComponent==>ngOnChanges'); } ngOnInit() { console.log('AppComponent==>ngOnInit'); } ngDoCheck() { console.log('AppComponent==>ngDoCheck'); } ngAfterContentInit() { console.log('AppComponent==>ngAfterContentInit'); } ngAfterContentChecked() { console.log('AppComponent==>ngAfterContentChecked'); } ngAfterViewInit() { console.log('AppComponent==>AfterViewInit'); } ngAfterViewChecked() { console.log('AppComponent==>AfterViewChecked'); this.message=this.viewChild.message; } }
- By placing the <b>Injected/<b> content from the <i>Parent</i> within the element tag, we are injecting material into <app-child>.
- To update the reference to the child component in the property ChildComponent, use the ViewChild query.
- this.message=this.viewChild.message; this component's message property is updated with that of ChildComponent
On Component Creation
- OnChanges
- OnInit
- DoCheck
- AfterContentInit
- AfterContentChecked
- AfterViewInit
- AfterViewChecked
On Component Running
- DoCheck
- AfterContentChecked
- AfterViewChecked
Content is first
AfterViewInit & AfterViewChecked fires after child components are ready
Init Hook fires only once
Avoid using Checked Hooks
Do not modify bindings in Checked Hooks
ngAfterViewChecked() { console.log('AppComponent==>AfterViewChecked'); this.message=this.viewChild.message; }
Now add the following to the app.component.ts template.
message from child {{message}}
- Before refreshing the message in app.component.ts, it waits for a tick.
- ExpressionChangedAfterItHasBeenCheckedError
- The value of the message is initially null.
- In the input element, we type h. A change detection cycle is started as a result of this.
- It examines the message variable's value. Its value is null. As a result, the DOM is updated with an empty string.
- The AfterViewChecked hook is fired by Angular.
- The message variable is changed to h.
- Angular then does the last check to ensure that all of the binding values are valid. If it detects that the message value is now h, it is not the same as when it examined it in step 3. The ExpressionChangedAfterItHasBeenCheckedError is raised.
- The change detection cycle comes to an end.
- In the input element, we type e.
- The cycle of change detection begins.
- It examines the message variable's value. It has the value of h. As a result, Angular uses h to update the DOM.
- The AfterViewChecked hook is fired by Angular.
- The message variable is changed to him.
- Angular then does the last check to ensure that all of the binding values are valid. If it detects that the message value is now h, it is not the same as when it examined it in step 3. The ExpressionChangedAfterItHasBeenCheckedError is raised.
- The change detection cycle comes to an end.