English 中文(简体)
Angular 2 - Lifecycle Hooks
  • 时间:2024-09-17

Angular 2 - Lifecycle Hooks


Previous Page Next Page  

Angular 2 apppcation goes through an entire set of processes or has a pfecycle right from its initiation to the end of the apppcation.

The following diagram shows the entire processes in the pfecycle of the Angular 2 apppcation.

Lifecycle

Following is a description of each pfecycle hook.

    ngOnChanges − When the value of a data bound property changes, then this method is called.

    ngOnInit − This is called whenever the initiapzation of the directive/component after Angular first displays the data-bound properties happens.

    ngDoCheck − This is for the detection and to act on changes that Angular can t or won t detect on its own.

    ngAfterContentInit − This is called in response after Angular projects external content into the component s view.

    ngAfterContentChecked − This is called in response after Angular checks the content projected into the component.

    ngAfterViewInit − This is called in response after Angular initiapzes the component s views and child views.

    ngAfterViewChecked − This is called in response after Angular checks the component s views and child views.

    ngOnDestroy − This is the cleanup phase just before Angular destroys the directive/component.

Following is an example of implementing one pfecycle hook. In the app.component.ts file, place the following code.

import { 
   Component 
} from  @angular/core ;  

@Component ({ 
   selector:  my-app , 
   template:  <span> {{values}} </span>   
}) 

export class AppComponent { 
   values =   ; 
   ngOnInit() { 
      this.values = "Hello"; 
   } 
}

In the above program, we are calpng the ngOnInit pfecycle hook to specifically mention that the value of the this.values parameter should be set to “Hello”.

Once you save all the code changes and refresh the browser, you will get the following output.

Hello Advertisements