@title
Lifecycle Hooks
@intro
Angular calls lifecycle hook methods on directives and components as it creates, changes, and destroys them.
@description
A component has a lifecycle managed by Angular.
Angular creates it, renders it, creates and renders its children,
checks it when its data-bound properties change, and destroys it before removing it from the DOM.
Angular offers **lifecycle hooks**
that provide visibility into these key life moments and the ability to act when they occur.
A directive has the same set of lifecycle hooks, minus the hooks that are specific to component content and views.
{@a hooks-overview}
## Component lifecycle hooks overview
Directive and component instances have a lifecycle
as Angular creates, updates, and destroys them.
Developers can tap into key moments in that lifecycle by implementing
one or more of the *lifecycle hook* interfaces in the Angular `core` library.
Each interface has a single hook method whose name is the interface name prefixed with `ng`.
For example, the `OnInit` interface has a hook method named `ngOnInit()`
that Angular calls shortly after creating the component:
Hook | Purpose and Timing |
---|---|
ngOnChanges()
|
Respond when Angular (re)sets data-bound input properties. The method receives a `SimpleChanges` object of current and previous property values. Called before `ngOnInit()` and whenever one or more data-bound input properties change. |
ngOnInit()
|
Initialize the directive/component after Angular first displays the data-bound properties and sets the directive/component's input properties. Called _once_, after the _first_ `ngOnChanges()`. |
ngDoCheck()
|
Detect and act upon changes that Angular can't or won't detect on its own. Called during every change detection run, immediately after `ngOnChanges()` and `ngOnInit()`. |
ngAfterContentInit()
|
Respond after Angular projects external content into the component's view. Called _once_ after the first `ngDoCheck()`. _A component-only hook_. |
ngAfterContentChecked()
|
Respond after Angular checks the content projected into the component. Called after the `ngAfterContentInit()` and every subsequent `ngDoCheck()`. _A component-only hook_. |
ngAfterViewInit()
|
Respond after Angular initializes the component's views and child views. Called _once_ after the first `ngAfterContentChecked()`. _A component-only hook_. |
ngAfterViewChecked()
|
Respond after Angular checks the component's views and child views. Called after the `ngAfterViewInit` and every subsequent `ngAfterContentChecked()`. _A component-only hook_. |
ngOnDestroy
|
Cleanup just before Angular destroys the directive/component. Unsubscribe Observables and detach event handlers to avoid memory leaks. Called _just before_ Angular destroys the directive/component. |
Component | Description |
---|---|
Peek-a-boo | Demonstrates every lifecycle hook. Each hook method writes to the on-screen log. |
Spy |
Directives have lifecycle hooks too.
A `SpyDirective` can log when the element it spies upon is
created or destroyed using the `ngOnInit` and `ngOnDestroy` hooks.
This example applies the `SpyDirective` to a ` ` in an `ngFor` *hero* repeater
managed by the parent `SpyComponent`.
|
OnChanges | See how Angular calls the `ngOnChanges()` hook with a `changes` object every time one of the component input properties changes. Shows how to interpret the `changes` object. |
DoCheck | Implements an `ngDoCheck()` method with custom change detection. See how often Angular calls this hook and watch it post changes to a log. |
AfterView | Shows what Angular means by a *view*. Demonstrates the `ngAfterViewInit` and `ngAfterViewChecked` hooks. |
AfterContent | Shows how to project external content into a component and how to distinguish projected content from a component's view children. Demonstrates the `ngAfterContentInit` and `ngAfterContentChecked` hooks. |
Counter | Demonstrates a combination of a component and a directive each with its own hooks. In this example, a `CounterComponent` logs a change (via `ngOnChanges`) every time the parent component increments its input counter property. Meanwhile, the `SpyDirective` from the previous example is applied to the `CounterComponent` log where it watches log entries being created and destroyed. |