What is the sequence of Angular lifecycle hooks?
Angular calls the lifecycle hook methods in the following sequence after creating a component/directive in Angular 6 by calling its constructor:
- ngOnChanges()
This responds when Angular re-sets data-bound input properties. - ngOnInit()
Used to initialize the directive/component after Angular first displays the data-bound properties while setting the directive/component's input properties. - ngDoCheck()
Called during every change detection run, immediately after ngOnChanges() and ngOnInit() to detect and act upon changes that Angular won’t do on its own - ngAfterContentInit()
Called once after the first ngDoCheck() to respond after Angular projects external content into the component’s/directive’s view - ngAfterContentChecked()
Called after the ngAfterContentInit() and every subsequent ngDoCheck() to respond after Angular checks the projected content. - ngAfterViewInit()
Called once after the first ngAfterContentChecked() to respond after Angular validates the component and it’s child views. - ngAfterViewChecked()
Called after the ngAfterViewInit() and after every subsequent ngAfterContentChecked(). - ngOnDestroy()
Called just before Angular destroys the directive/component to avoid memory leaks by effective cleanup just before destroying the directives.
BY Best Interview Question ON 10 Nov 2020