๐Ÿš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
๐ŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

Component Lifecycle in Angular

Master the sequence of Angular lifecycle events, from initial property changes to final destruction and cleanup.

โšก Total XP: 0|๐Ÿ’ป angular XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Select an unlocked node to view details root

๐Ÿš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
๐ŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

Understanding when Angular executes your code is vital for building bug-free applications. The lifecycle hooks provide visibility into the component's internal state.

1The Sequence of Events

A component's life starts with the constructor, followed immediately by ngOnChanges. Once inputs are settled, ngOnInit runs. After the view is composed, ngAfterViewInit fires. This sequence is deterministic. If you try to access a @ViewChild in ngOnInit, it will likely be undefined because the view hasn't been initialized yet. Mastering this order prevents the 'ExpressionChangedAfterItHasBeenCheckedError' and other common pitfalls.

2Strategic Cleanup

The ngOnDestroy hook is the most important for application health. In a single-page application, components are frequently created and destroyed as the user navigates. If you subscribe to a global stream or set a setInterval in a component, that work continues even after the component is gone unless you manually stop it in ngOnDestroy. This is the single biggest cause of memory leaks and performance degradation in large-scale Angular apps.

3Step-by-Step Breakdown

Components aren't static. They are born, they change, and they die. Angular lets you 'hook' into these moments.

The first major hook is 'ngOnInit'. It's the best place to initialize data or fetch information from a service.

If your component has @Input() properties, 'ngOnChanges' runs every time those values change. It gives you the old and new values.

Checkpoint: Which lifecycle hook is the most appropriate for performing one-time initialization, such as calling a service?

  • โ†’ngOnInit
  • โ†’ngOnChanges

When you need to interact with the DOM or child components, use 'ngAfterViewInit'. This runs after the template is fully rendered.

Finally, 'ngOnDestroy' is your cleanup crew. Use it to unsubscribe from Observables or clear timers before the component is removed.

Checkpoint: What is the primary purpose of the ngOnDestroy hook?

  • โ†’Initialize variables
  • โ†’Perform cleanup and prevent leaks

Lifecycle mastered! You now know exactly when and where to put your code for maximum performance and stability.

Next, we'll wrap up the Angular core by looking at Testing and Production Deployment.

Level Up ๐Ÿš€

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1`ngAfterViewInit` Is the Right Place to Move Initial Focus

If a component (like a modal) needs to programmatically focus an element as soon as it renders, `ngAfterViewInit` is the correct hook โ€” it fires only after the view (and any `@ViewChild` references) is fully initialized, unlike `ngOnInit`, which runs before the view exists.

2`ngOnDestroy` Is Where You Clean Up Anything That Could Leave Assistive Tech in a Bad State

If a component registered a global keydown listener or an `aria-live` region reference, `ngOnDestroy` is where that must be torn down โ€” leaving it dangling after the component's DOM is gone can cause confusing behavior for keyboard and screen reader users navigating elsewhere in the app.

SEO Implications

  • 1

    Under SSR, Certain Lifecycle Hooks Behave Differently or Don't Fire the Same Way

    Some browser-only lifecycle-triggered behavior (like measuring an element's rendered size in `ngAfterViewInit`) doesn't make sense or work identically during Angular Universal's server-side render pass โ€” code relying on browser globals within lifecycle hooks needs platform guards.

  • 2

    Fetching Critical Content in `ngOnInit` Ties Its SEO Visibility to SSR Timing

    If `ngOnInit` triggers an HTTP request for content that should be indexable, that request must actually resolve during the server-side render (using Angular's `TransferState` or an SSR-aware data resolver) for the content to appear in the HTML a crawler receives.

Best Practices

Use `ngOnChanges` Instead of `ngOnInit` to React to Input Changes After the First Render

`ngOnInit` only runs once, right after the first `ngOnChanges` โ€” if a component needs to react every time an `@Input()` value changes later, `ngOnChanges` (or a getter/setter on the input) is the hook actually designed for that, not `ngOnInit`.

Always Pair Setup Logic With Matching Teardown Logic in `ngOnDestroy`

Any subscription started in `ngOnInit`, timer set with `setInterval`, or global event listener added anywhere in the component's life should have a matching cleanup in `ngOnDestroy` โ€” treat the two hooks as a pair, not `ngOnDestroy` as an afterthought.

Frequent Bugs

THE BUG

A component doesn't react when a parent updates one of its `@Input()` bindings after the initial render.

THE FIX

The reactive logic was placed in `ngOnInit`, which only ever runs once. Move it to `ngOnChanges`, which fires on every subsequent input change, or use an input setter that runs custom logic whenever a new value is assigned.

THE BUG

A `setInterval` or subscription set up in a component keeps running and causing errors long after the component has been navigated away from and destroyed.

THE FIX

The corresponding cleanup was never added to `ngOnDestroy` โ€” clear the interval with `clearInterval`, and unsubscribe from any manually-created subscriptions, in that hook to match the setup that happened earlier in the component's life.

Real-World Examples

Modal Component With Correct Focus and Cleanup Lifecycle

A modal component moves focus to itself once its view is ready (not before), and cleans up a document-level keydown listener when destroyed, preventing it from firing after the modal is gone.

ngAfterViewInit() { this.modalRef.nativeElement.focus(); }
ngOnDestroy() { document.removeEventListener('keydown', this.escHandler); }

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Memory leaks from unclosed Subscriptions

// Wrong ngOnInit() { this.dataService.getData().subscribe(data => this.data = data); } // Correct ngOnInit() { this.sub = this.dataService.getData().subscribe(data => this.data = data); } ngOnDestroy() { if (this.sub) this.sub.unsubscribe(); }

The Solution //

When subscribing to Observables in a component, always unsubscribe in the ngOnDestroy hook to prevent memory leaks.

The Error //

Directly manipulating the DOM

// Wrong document.getElementById('my-el').style.color = 'red'; // Correct @ViewChild('myEl') myEl: ElementRef; this.renderer.setStyle(this.myEl.nativeElement, 'color', 'red');

The Solution //

Avoid using document.getElementById or native DOM APIs. Use Angular's templating, bindings, and tools like Renderer2 or ViewChild.

Lesson Glossary

[01]ngOnInit

A hook that runs once after Angular has finished initializing the component's data-bound properties.

Code Preview
ngOnInit

[02]ngOnChanges

A hook that runs whenever one or more data-bound input properties change.

Code Preview
ngOnChanges

[03]ngOnDestroy

A hook that runs just before Angular destroys the component; used for cleanup.

Code Preview
ngOnDestroy

[04]ngAfterViewInit

A hook that runs after Angular has fully initialized the component's view and its child views.

Code Preview
ngAfterViewInit

[05]SimpleChanges

An object passed to ngOnChanges that contains the previous and current values of changed inputs.

Code Preview
SimpleChanges

[06]Hook

An interface method that allows you to intercept a specific moment in the component lifecycle.

Code Preview
LifecycleHook

Continue Learning