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

前端 未结 3 627
失恋的感觉
失恋的感觉 2021-01-30 09:55

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

I found the only difference between them is @ViewChild

3条回答
  •  Happy的楠姐
    2021-01-30 10:41

    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.

提交回复
热议问题