Sitemap

Member-only story

Angular 2 Lifecycle

2 min readNov 15, 2020

When a component is placed on page, angular goes through a few methods before it is actually placed on the page. These are called lifecyle methods, these methods allows us to tap into a few of those hooks and initialize/update code/content while in that phase of the step.

Phases from initialization to destruction of an angular component

constructor — this is invoked when Angular creates a component or directive by calling the new on the class.

ngOnChanges — called after a bound input property changes. Executed right at the start and anytime the property with these decorated with this method changes.

ngOnInit — Called once the component is initialized after the constructor.

ngDoCheck — This method will be run a lot. Called during every change detection within the component. Even when an end user clicks randomly on a component, it will fire this method even if we don’t have any binding on a click event.

Note: When implementing these hooks, we should not implement ngDoCheck & ngOnChanges together on the same component.

ngAfterContentInit — Called after content (ng-content) has been rendered into the view

ngAfterContentChecked — Called every time the child content has been checked.

ngAfterViewInit — Called after the component’s view (including child views) has been initialized