Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between ngOnInit and ngAfterViewInit of Angular2?

I can not understand what the difference between ngOnInit and ngAfterViewInit.

I found the only difference between them is @ViewChild. According to the following code, the elementRef.nativeElement in them are the same.

What scene should we use ngAfterViewInit?

@Component({   selector: 'my-child-view',   template: `   <div id="my-child-view-id">{{hero}}</div>   ` }) export class ChildViewComponent {   @Input() hero: string = 'Jack'; }  ////////////////////// @Component({   selector: 'after-view',   template: `     <div id="after-view-id">-- child view begins --</div>       <my-child-view [hero]="heroName"></my-child-view>     <div>-- child view ends --</div>`     + `     <p *ngIf="comment" class="comment">       {{comment}}     </p>   ` }) export class AfterViewComponent implements AfterViewInit, OnInit {   private prevHero = '';   public heroName = 'Tom';   public comment = '';    // Query for a VIEW child of type `ChildViewComponent`   @ViewChild(ChildViewComponent) viewChild: ChildViewComponent;    constructor(private logger: LoggerService, private elementRef: ElementRef) {   }    ngOnInit() {     console.log('OnInit');     console.log(this.elementRef.nativeElement.querySelector('#my-child-view-id'));     console.log(this.elementRef.nativeElement.querySelector('#after-view-id'));     console.log(this.viewChild);     console.log(this.elementRef.nativeElement.querySelector('p'));   }    ngAfterViewInit() {     console.log('AfterViewInit');     console.log(this.elementRef.nativeElement.querySelector('#my-child-view-id'));     console.log(this.elementRef.nativeElement.querySelector('#after-view-id'));     console.log(this.viewChild);     console.log(this.elementRef.nativeElement.querySelector('p'));   } } 
like image 934
Zhiyuan Sun Avatar asked Nov 26 '16 10:11

Zhiyuan Sun


People also ask

When should you use ngAfterViewInit?

ngAfterViewInit is useful when you want to call a lifecycle hook after all child components have been initialized and checked.

What is ngAfterViewInit?

ngAfterViewInit()linkA callback method that is invoked immediately after Angular has completed initialization of a component's view. It is invoked only once when the view is instantiated.

What is difference between ngAfterContentInit and ngAfterViewInit?

ngAfterContentInit : This is called after components external content has been initialized. ngAfterViewInit : This is called after the component view and its child views has been initialized.

Why ngAfterViewInit is not called?

ngAfterViewInit() is called once after ngAfterContentChecked(). ngAfterViewInit() is called after all child components are initialized and checked. ngAfterViewInit() is called after the view is initially rendered. This is why @ViewChild() depends on it.


2 Answers

ngOnInit() is called after ngOnChanges() was called the first time. ngOnChanges() is called every time inputs are updated by change detection.

ngAfterViewInit() is called after the view is initially rendered. This is why @ViewChild() depends on it. You can't access view members before they are rendered.

like image 122
Günter Zöchbauer Avatar answered Sep 22 '22 19:09

Günter Zöchbauer


ngOnInit() is called right after the directive's data-bound properties have been checked for the first time, and before any of its children have been checked. It is invoked only once when the directive is instantiated.

ngAfterViewInit() is called after a component's view, and its children's views, are created. Its a lifecycle hook that is called after a component's view has been fully initialized.

like image 45
Peter Avatar answered Sep 24 '22 19:09

Peter