{ "id": "guide/lifecycle-hooks", "title": "Lifecycle hooks", "contents": "\n\n\n
\n mode_edit\n
\n\n\n
\n

Lifecycle hookslink

\n

A component instance has a lifecycle that starts when Angular instantiates the component class and renders the component view along with its child views.\nThe lifecycle continues with change detection, as Angular checks to see when data-bound properties change, and updates both the view and the component instance as needed.\nThe lifecycle ends when Angular destroys the component instance and removes its rendered template from the DOM.\nDirectives have a similar lifecycle, as Angular creates, updates, and destroys instances in the course of execution.

\n

Your application can use lifecycle hook methods to tap into key events in the lifecycle of a component or directive in order to initialize new instances, initiate change detection when needed, respond to updates during change detection, and clean up before deletion of instances.

\n

Prerequisiteslink

\n

Before working with lifecycle hooks, you should have a basic understanding of the following:

\n\n\n

Responding to lifecycle eventslink

\n

You can respond to events in the lifecycle of a component or directive by implementing one or more of the lifecycle hook interfaces in the Angular core library.\nThe hooks give you the opportunity to act on a component or directive instance at the appropriate moment, as Angular creates, updates, or destroys that instance.

\n

Each interface defines the prototype for a single hook method, whose name is the interface name prefixed with ng.\nFor example, the OnInit interface has a hook method named ngOnInit(). If you implement this method in your component or directive class, Angular calls it shortly after checking the input properties for that component or directive for the first time.

\n\n@Directive()\nexport class PeekABooDirective implements OnInit {\n constructor(private logger: LoggerService) { }\n\n // implement OnInit's `ngOnInit` method\n ngOnInit() {\n this.logIt(`OnInit`);\n }\n\n logIt(msg: string) {\n this.logger.log(`#${nextId++} ${msg}`);\n }\n}\n\n\n

You don't have to implement all (or any) of the lifecycle hooks, just the ones you need.

\n\n

Lifecycle event sequencelink

\n

After your application instantiates a component or directive by calling its constructor, Angular calls the hook methods you have implemented at the appropriate point in the lifecycle of that instance.

\n

Angular executes hook methods in the following sequence. You can use them to perform the following kinds of operations.

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Hook methodPurposeTiming
\n ngOnChanges()\n \n

Respond when Angular sets or resets data-bound input properties.\nThe method receives a SimpleChanges object of current and previous property values.

\n

Note that this happens very frequently, so any operation you perform here impacts performance significantly.\nSee details in Using change detection hooks in this document.

\n
\n

Called before ngOnInit() and whenever one or more data-bound input properties change.

\n

Note that if your component has no inputs or you use it without providing any inputs, the framework will not call ngOnChanges().

\n
\n ngOnInit()\n \n

Initialize the directive or component after Angular first displays the data-bound properties\nand sets the directive or component's input properties.\nSee details in Initializing a component or directive in this document.

\n
\n

Called once, after the first ngOnChanges().

\n
\n ngDoCheck()\n \n

Detect and act upon changes that Angular can't or won't detect on its own.\nSee details and example in Defining custom change detection in this document.

\n
\n

Called immediately after ngOnChanges() on every change detection run, and immediately after ngOnInit() on the first run.

\n
\n ngAfterContentInit()\n \n

Respond after Angular projects external content into the component's view, or into the view that a directive is in.

\n

See details and example in Responding to changes in content in this document.

\n
\n

Called once after the first ngDoCheck().

\n
\n ngAfterContentChecked()\n \n

Respond after Angular checks the content projected into the directive or component.

\n

See details and example in Responding to projected content changes in this document.

\n
\n

Called after ngAfterContentInit() and every subsequent ngDoCheck().

\n
\n ngAfterViewInit()\n \n

Respond after Angular initializes the component's views and child views, or the view that contains the directive.

\n

See details and example in Responding to view changes in this document.

\n
\n

Called once after the first ngAfterContentChecked().

\n
\n ngAfterViewChecked()\n \n

Respond after Angular checks the component's views and child views, or the view that contains the directive.

\n
\n

Called after the ngAfterViewInit() and every subsequent ngAfterContentChecked().

\n
\n ngOnDestroy()\n \n

Cleanup just before Angular destroys the directive or component.\nUnsubscribe Observables and detach event handlers to avoid memory leaks.\nSee details in Cleaning up on instance destruction in this document.

\n
\n

Called immediately before Angular destroys the directive or component.

\n
\n\n

Lifecycle example setlink

\n

The \ndemonstrates the use of lifecycle hooks through a series of exercises\npresented as components under the control of the root AppComponent.\nIn each case a parent component serves as a test rig for\na child component that illustrates one or more of the lifecycle hook methods.

\n

The following table lists the exercises with brief descriptions.\nThe sample code is also used to illustrate specific tasks in the following sections.

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ComponentDescription
\n Peek-a-boo\n \n

Demonstrates every lifecycle hook.\nEach hook method writes to the on-screen log.

\n
\n Spy\n \n

Shows how you can use lifecycle hooks with a custom directive.\nThe SpyDirective implements the ngOnInit() and ngOnDestroy() hooks,\nand uses them to watch and report when an element goes in or out of the current view.

\n
\n OnChanges\n \n

Demonstrates how Angular calls the ngOnChanges() hook\nevery time one of the component input properties changes,\nand shows how to interpret the changes object passed to the hook method.

\n
\n DoCheck\n \n

Implements the ngDoCheck() method with custom change detection.\nWatch the hook post changes to a log to see how often Angular calls this hook.

\n
\n AfterView\n \n

Shows what Angular means by a view.\nDemonstrates the ngAfterViewInit() and ngAfterViewChecked() hooks.

\n
\n AfterContent\n \n

Shows how to project external content into a component and\nhow to distinguish projected content from a component's view children.\nDemonstrates the ngAfterContentInit() and ngAfterContentChecked() hooks.

\n
\n Counter\n \n

Demonstrates a combination of a component and a directive, each with its own hooks.

\n
\n\n

Initializing a component or directivelink

\n

Use the ngOnInit() method to perform the following initialization tasks.

\n\n\n\n

Cleaning up on instance destructionlink

\n

Put cleanup logic in ngOnDestroy(), the logic that must run before Angular destroys the directive.

\n

This is the place to free resources that won't be garbage-collected automatically.\nYou risk memory leaks if you neglect to do so.

\n\n

The ngOnDestroy() method is also the time to notify another part of the application that the component is going away.

\n

General exampleslink

\n

The following examples demonstrate the call sequence and relative frequency of the various lifecycle events, and how the hooks can be used separately or together for components and directives.

\n\n

Sequence and frequency of all lifecycle eventslink

\n

To show how Angular calls the hooks in the expected order, the PeekABooComponent demonstrates all of the hooks in one component.

\n

In practice you would rarely, if ever, implement all of the interfaces the way this demo does.

\n

The following snapshot reflects the state of the log after the user clicked the Create... button and then the Destroy... button.

\n
\n \"Peek-a-boo\"\n
\n

The sequence of log messages follows the prescribed hook calling order:\nOnChanges, OnInit, DoCheck (3x), AfterContentInit, AfterContentChecked (3x),\nAfterViewInit, AfterViewChecked (3x), and OnDestroy.

\n
\n

Notice that the log confirms that input properties (the name property in this case) have no assigned values at construction.\nThe input properties are available to the onInit() method for further initialization.

\n
\n

Had the user clicked the Update Hero button, the log would show another OnChanges and two more triplets of DoCheck, AfterContentChecked and AfterViewChecked.\nNotice that these three hooks fire often, so it is important to keep their logic as lean as possible.

\n\n

Use directives to watch the DOMlink

\n

The Spy example demonstrates how you can use hook method for directives as well as components.\nThe SpyDirective implements two hooks, ngOnInit() and ngOnDestroy(), in order to discover when a watched element is in the current view.

\n

This template applies the SpyDirective to a <div> in the ngFor hero repeater managed by the parent SpyComponent.

\n

The example does not perform any initialization or clean-up.\nIt just tracks the appearance and disappearance of an element in the view by recording when the directive itself is instantiated and destroyed.

\n

A spy directive like this can provide insight into a DOM object that you cannot change directly.\nYou can't touch the implementation of a native <div>, or modify a third party component.\nYou can, however watch these elements with a directive.

\n

The directive defines ngOnInit() and ngOnDestroy() hooks\nthat log messages to the parent via an injected LoggerService.

\n\nlet nextId = 1;\n\n// Spy on any element to which it is applied.\n// Usage: <div appSpy>...</div>\n@Directive({selector: '[appSpy]'})\nexport class SpyDirective implements OnInit, OnDestroy {\n private id = nextId++;\n\n constructor(private logger: LoggerService) { }\n\n ngOnInit() {\n this.logger.log(`Spy #${this.id} onInit`);\n }\n\n ngOnDestroy() {\n this.logger.log(`Spy #${this.id} onDestroy`);\n }\n}\n\n\n

You can apply the spy to any native or component element, and see that it is initialized and destroyed\nat the same time as that element.\nHere it is attached to the repeated hero <div>:

\n\n<div *ngFor=\"let hero of heroes\" appSpy class=\"heroes\">\n {{hero}}\n</div>\n\n\n

Each spy's creation and destruction marks the appearance and disappearance of the attached hero <div>\nwith an entry in the Hook Log as seen here:

\n
\n \"Spy\n
\n

Adding a hero results in a new hero <div>. The spy's ngOnInit() logs that event.

\n

The Reset button clears the heroes list.\nAngular removes all hero <div> elements from the DOM and destroys their spy directives at the same time.\nThe spy's ngOnDestroy() method reports its last moments.

\n\n

Use component and directive hooks togetherlink

\n

In this example, a CounterComponent uses the ngOnChanges() method to log a change every time the parent component increments its input counter property.

\n

This example applies the SpyDirective from the previous example to the CounterComponent log, in order to watch the creation and destruction of log entries.

\n\n

Using change detection hookslink

\n

Angular calls the ngOnChanges() method of a component or directive whenever it detects changes to the input properties.\nThe onChanges example demonstrates this by monitoring the OnChanges() hook.

\n\nngOnChanges(changes: SimpleChanges) {\n for (const propName in changes) {\n const chng = changes[propName];\n const cur = JSON.stringify(chng.currentValue);\n const prev = JSON.stringify(chng.previousValue);\n this.changeLog.push(`${propName}: currentValue = ${cur}, previousValue = ${prev}`);\n }\n}\n\n\n

The ngOnChanges() method takes an object that maps each changed property name to a\nSimpleChange object holding the current and previous property values.\nThis hook iterates over the changed properties and logs them.

\n

The example component, OnChangesComponent, has two input properties: hero and power.

\n\n@Input() hero: Hero;\n@Input() power: string;\n\n\n

The host OnChangesParentComponent binds to them as follows.

\n\n<on-changes [hero]=\"hero\" [power]=\"power\"></on-changes>\n\n\n

Here's the sample in action as the user makes changes.

\n
\n \"OnChanges\"\n
\n

The log entries appear as the string value of the power property changes.\nNotice, however, that the ngOnChanges() method does not catch changes to hero.name.\nThis is because Angular calls the hook only when the value of the input property changes.\nIn this case, hero is the input property, and the value of the hero property is the reference to the hero object.\nThe object reference did not change when the value of its own name property changed.

\n\n

Responding to view changeslink

\n

As Angular traverses the view hierarchy during change detection, it needs to be sure that a change in a child does not attempt to cause a change in its own parent. Such a change would not be rendered properly, because of how unidirectional data flow works.

\n

If you need to make a change that inverts the expected data flow, you must trigger a new change detection cycle to allow that change to be rendered.\nThe examples illustrate how to make such changes safely.

\n

The AfterView sample explores the AfterViewInit() and AfterViewChecked() hooks that Angular calls\nafter it creates a component's child views.

\n

Here's a child view that displays a hero's name in an <input>:

\n\n@Component({\n selector: 'app-child-view',\n template: '<input [(ngModel)]=\"hero\">'\n})\nexport class ChildViewComponent {\n hero = 'Magneta';\n}\n\n\n

The AfterViewComponent displays this child view within its template:

\n\ntemplate: `\n <div>-- child view begins --</div>\n <app-child-view></app-child-view>\n <div>-- child view ends --</div>`\n\n\n

The following hooks take action based on changing values within the child view,\nwhich can only be reached by querying for the child view via the property decorated with\n@ViewChild.

\n\nexport class AfterViewComponent implements AfterViewChecked, AfterViewInit {\n private prevHero = '';\n\n // Query for a VIEW child of type `ChildViewComponent`\n @ViewChild(ChildViewComponent) viewChild: ChildViewComponent;\n\n ngAfterViewInit() {\n // viewChild is set after the view has been initialized\n this.logIt('AfterViewInit');\n this.doSomething();\n }\n\n ngAfterViewChecked() {\n // viewChild is updated after the view has been checked\n if (this.prevHero === this.viewChild.hero) {\n this.logIt('AfterViewChecked (no change)');\n } else {\n this.prevHero = this.viewChild.hero;\n this.logIt('AfterViewChecked');\n this.doSomething();\n }\n }\n // ...\n}\n\n\n\n

Wait before updating the viewlink

\n

In this example, the doSomething() method updates the screen when the hero name exceeds 10 characters, but waits a tick before updating comment.

\n\n// This surrogate for real business logic sets the `comment`\nprivate doSomething() {\n const c = this.viewChild.hero.length > 10 ? `That's a long name` : '';\n if (c !== this.comment) {\n // Wait a tick because the component's view has already been checked\n this.logger.tick_then(() => this.comment = c);\n }\n}\n\n\n

Both the AfterViewInit() and AfterViewChecked() hooks fire after the component's view has been composed.\nIf you modify the code so that the hook updates the component's data-bound comment property immediately, you can see that Angular throws an error.

\n

The LoggerService.tick_then() statement postpones the log update\nfor one turn of the browser's JavaScript cycle, which triggers a new change-detection cycle.

\n

Write lean hook methods to avoid performance problemslink

\n

When you run the AfterView sample, notice how frequently Angular calls AfterViewChecked()-often when there are no changes of interest.\nBe very careful about how much logic or computation you put into one of these methods.

\n
\n \"AfterView\"\n
\n\n\n\n

Responding to projected content changeslink

\n

Content projection is a way to import HTML content from outside the component and insert that content\ninto the component's template in a designated spot.\nYou can identify content projection in a template by looking for the following constructs.

\n\n
\n

AngularJS developers know this technique as transclusion.

\n
\n

The AfterContent sample explores the AfterContentInit() and AfterContentChecked() hooks that Angular calls after Angular projects external content into the component.

\n

Consider this variation on the previous AfterView example.\nThis time, instead of including the child view within the template, it imports the content from\nthe AfterContentComponent's parent.\nThe following is the parent's template.

\n\n`<after-content>\n <app-child></app-child>\n</after-content>`\n\n\n

Notice that the <app-child> tag is tucked between the <after-content> tags.\nNever put content between a component's element tags unless you intend to project that content\ninto the component.

\n

Now look at the component's template.

\n\ntemplate: `\n <div>-- projected content begins --</div>\n <ng-content></ng-content>\n <div>-- projected content ends --</div>`\n\n\n

The <ng-content> tag is a placeholder for the external content.\nIt tells Angular where to insert that content.\nIn this case, the projected content is the <app-child> from the parent.

\n
\n \"Projected\n
\n

Using AfterContent hookslink

\n

AfterContent hooks are similar to the AfterView hooks.\nThe key difference is in the child component.

\n\n

The following AfterContent hooks take action based on changing values in a content child,\nwhich can only be reached by querying for them via the property decorated with\n@ContentChild.

\n\nexport class AfterContentComponent implements AfterContentChecked, AfterContentInit {\n private prevHero = '';\n comment = '';\n\n // Query for a CONTENT child of type `ChildComponent`\n @ContentChild(ChildComponent) contentChild: ChildComponent;\n\n ngAfterContentInit() {\n // contentChild is set after the content has been initialized\n this.logIt('AfterContentInit');\n this.doSomething();\n }\n\n ngAfterContentChecked() {\n // contentChild is updated after the content has been checked\n if (this.prevHero === this.contentChild.hero) {\n this.logIt('AfterContentChecked (no change)');\n } else {\n this.prevHero = this.contentChild.hero;\n this.logIt('AfterContentChecked');\n this.doSomething();\n }\n }\n // ...\n}\n\n\n\n
\n
No need to wait for content updates
\n

This component's doSomething() method updates the component's data-bound comment property immediately.\nThere's no need to delay the update to ensure proper rendering.

\n

Angular calls both AfterContent hooks before calling either of the AfterView hooks.\nAngular completes composition of the projected content before finishing the composition of this component's view.\nThere is a small window between the AfterContent... and AfterView... hooks that allows you to modify the host view.

\n
\n\n

Defining custom change detectionlink

\n

To monitor changes that occur where ngOnChanges() won't catch them, you can implement your own change check, as shown in the DoCheck example.\nThis example shows how you can use the ngDoCheck() hook to detect and act upon changes that Angular doesn't catch on its own.

\n

The DoCheck sample extends the OnChanges sample with the following ngDoCheck() hook:

\n\nngDoCheck() {\n\n if (this.hero.name !== this.oldHeroName) {\n this.changeDetected = true;\n this.changeLog.push(`DoCheck: Hero name changed to \"${this.hero.name}\" from \"${this.oldHeroName}\"`);\n this.oldHeroName = this.hero.name;\n }\n\n if (this.power !== this.oldPower) {\n this.changeDetected = true;\n this.changeLog.push(`DoCheck: Power changed to \"${this.power}\" from \"${this.oldPower}\"`);\n this.oldPower = this.power;\n }\n\n if (this.changeDetected) {\n this.noChangeCount = 0;\n } else {\n // log that hook was called when there was no relevant change.\n const count = this.noChangeCount += 1;\n const noChangeMsg = `DoCheck called ${count}x when no change to hero or power`;\n if (count === 1) {\n // add new \"no change\" message\n this.changeLog.push(noChangeMsg);\n } else {\n // update last \"no change\" message\n this.changeLog[this.changeLog.length - 1] = noChangeMsg;\n }\n }\n\n this.changeDetected = false;\n}\n\n\n

This code inspects certain values of interest, capturing and comparing their current state against previous values.\nIt writes a special message to the log when there are no substantive changes to the hero or the power so you can see how often DoCheck() is called.\nThe results are illuminating.

\n
\n \"DoCheck\"\n
\n

While the ngDoCheck() hook can detect when the hero's name has changed, it is very expensive.\nThis hook is called with enormous frequency—after every\nchange detection cycle no matter where the change occurred.\nIt's called over twenty times in this example before the user can do anything.

\n

Most of these initial checks are triggered by Angular's first rendering of unrelated data elsewhere on the page.\nJust moving the cursor into another <input> triggers a call.\nRelatively few calls reveal actual changes to pertinent data.\nIf you use this hook, your implementation must be extremely lightweight or the user experience suffers.

\n\n \n
\n\n\n" }