What is the component lifecycle? Describe the main hooks.

Angular components and templates · Freshers and experienced

Model answer

Angular creates, updates and destroys components and lets you run code at key moments. The main hooks are: ngOnChanges (when an input changes), ngOnInit (once, after the first inputs are set, the usual place for setup), ngDoCheck (each change detection run), ngAfterViewInit (after the view and child views exist), and ngOnDestroy (just before the component is removed, to clean up).

There are also afterNextRender and afterRender functions for code that must run after the DOM is updated, which is useful for browser-only work.

Tips

Learn this topic

Angular Tutorial: Beginner to Advanced .

Sign in to add your own answer.

More questions