Understanding the component lifecycle helps you know when and how your Angular component starts, updates, and ends. This lets you run code at the right time to make your app work smoothly.
Component lifecycle overview in Angular
import { Component, OnInit, OnChanges, OnDestroy, AfterViewInit, AfterContentInit } from '@angular/core'; @Component({ selector: 'app-example', template: `<p>Example works!</p>`, standalone: true }) export class ExampleComponent implements OnInit, OnChanges, AfterViewInit, AfterContentInit, OnDestroy { ngOnChanges() { // Called when input properties change } ngOnInit() { // Called once after first ngOnChanges } ngAfterContentInit() { // Called after content (ng-content) is projected } ngAfterViewInit() { // Called after component's view is initialized } ngOnDestroy() { // Called just before component is destroyed } }
Each lifecycle method runs at a specific time during the component's life.
Implement only the lifecycle hooks you need for your component.
export class MyComponent implements OnInit { ngOnInit() { console.log('Component initialized'); } }
export class MyComponent implements OnChanges { ngOnChanges() { console.log('Input changed'); } }
export class MyComponent implements OnDestroy { ngOnDestroy() { console.log('Component destroyed'); } }
This component logs messages when it initializes, when its input changes, and when it is destroyed. It shows how lifecycle hooks help track component events.
import { Component, Input, OnInit, OnChanges, OnDestroy, SimpleChanges } from '@angular/core'; @Component({ selector: 'app-lifecycle-demo', template: `<p>Message: {{message}}</p>`, standalone: true }) export class LifecycleDemoComponent implements OnInit, OnChanges, OnDestroy { @Input() message = ''; ngOnChanges(changes: SimpleChanges) { console.log('ngOnChanges called:', changes); } ngOnInit() { console.log('ngOnInit called'); } ngOnDestroy() { console.log('ngOnDestroy called'); } }
ngOnChanges runs before ngOnInit and whenever input properties change.
ngOnDestroy is useful to stop timers or unsubscribe from data streams.
Not all lifecycle hooks are needed in every component.
Angular components have special methods called lifecycle hooks.
These hooks run at key moments like creation, update, and destruction.
Use lifecycle hooks to run your code at the right time in the component's life.
Practice
Solution
Step 1: Understand lifecycle hook purpose
ngOnInit()runs once after Angular sets component input properties.Step 2: Compare other hooks
ngOnDestroy()runs before component removal,ngAfterViewInit()after view initialization,ngDoCheck()on every change detection.Final Answer:
ngOnInit() -> Option AQuick Check:
Initialization hook = ngOnInit() [OK]
- Confusing ngOnInit with ngAfterViewInit
- Thinking ngOnDestroy runs on init
- Mixing ngDoCheck with ngOnInit
OnDestroy lifecycle hook in an Angular component?Solution
Step 1: Recall correct method name
The lifecycle hook method for destruction isngOnDestroy().Step 2: Check interface implementation
ImplementingOnDestroyrequires definingngOnDestroy()exactly.Final Answer:
class MyComponent implements OnDestroy { ngOnDestroy() { } } -> Option BQuick Check:
OnDestroy method = ngOnDestroy() [OK]
- Using wrong method name like ngDestroy or destroy
- Not implementing the interface
- Omitting parentheses in method
export class SampleComponent implements OnInit, OnDestroy {
ngOnInit() {
console.log('Init');
}
ngOnDestroy() {
console.log('Destroy');
}
}Solution
Step 1: Understand ngOnInit behavior
ngOnInit()runs once when component initializes, logging 'Init'.Step 2: Understand ngOnDestroy behavior
ngOnDestroy()runs once before component is removed, logging 'Destroy'.Final Answer:
Init followed by Destroy -> Option CQuick Check:
Init then Destroy logs = Init followed by Destroy [OK]
- Assuming ngOnDestroy runs on init
- Expecting logs in reverse order
- Thinking no logs appear
export class TestComponent implements OnInit {
ngOnInit() {
console.log('Component initialized');
}
ngOnDestroy() {
console.log('Component destroyed');
}
}Solution
Step 1: Check implemented interfaces
The class implementsOnInitbut definesngOnDestroy()without implementingOnDestroy.Step 2: Understand interface contract
To usengOnDestroy(), the class should implementOnDestroyinterface for clarity and correctness.Final Answer:
Missing OnDestroy interface in implements clause -> Option AQuick Check:
Implement interface for each lifecycle hook used [OK]
- Ignoring interface implementation for ngOnDestroy
- Thinking method names are wrong
- Believing console.log is invalid
Solution
Step 1: Identify hook for view initialization
ngAfterViewInit()runs after Angular initializes the component's view and child views.Step 2: Compare other hooks
ngOnInit()runs earlier,ngAfterContentInit()runs after projected content, andngDoCheck()runs repeatedly.Final Answer:
ngAfterViewInit() because it runs after the component's view and child views are initialized. -> Option DQuick Check:
View init completion = ngAfterViewInit() [OK]
- Confusing ngAfterContentInit with ngAfterViewInit
- Using ngOnInit for view-dependent code
- Expecting ngDoCheck to run once
Ready to prove it?
Pass the test and this pattern moves unknown
