PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Angular · Theory

Lifecycle Hooks

← All stacks

Theory

21/43

Lifecycle Hooks

ngOnInit runs once after inputs are set — load marks here, not in the constructor. ngOnDestroy — stop timers, unsubscribe. ngOnChanges — when @Input changes.

Board: constructor only assigns. ngOnInit calls marksService.list(). ngOnDestroy clears a setInterval. Trap: HTTP in constructor — inputs may still be undefined.

Exam tip

Init vs constructor. Destroy cleanup.

Example

import { Component } from "@angular/core";

@Component({
  selector: "app-root",
  template: `
    <h1>{{ title }}</h1>
    <button (click)="toggle()">Toggle</button>
    <p *ngIf="open">Panel open</p>
  `,
})
export class AppComponent {
  title = "Angular practice";
  open = true;
  toggle() {
    this.open = !this.open;
  }
}

Lifecycle Hooks: toggle open. Say whether the panel is on screen after one click.

Short notes

  • DefngOnInit after inputs. ngOnDestroy cleanup.
  • RuleNo HTTP in constructor.
  • TrapTimer leak after leave.

Questions

1

ngOnInit vs constructor?

2

Why ngOnDestroy?

Previous← Observables BasicsNextStyling →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.