Get your own Angular server
main.ts
index.html
 
import { bootstrapApplication } from '@angular/platform-browser';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'child-cmp',
  standalone: true,
  template: `<p>Child active</p>`
})
export class Child implements OnInit, OnDestroy {
  intervalId;
  ngOnInit() {
    console.log('Child ngOnInit');
    this.intervalId = setInterval(() => console.log('Child alive'), 1000);
  }
  ngOnDestroy() {
    console.log('Child ngOnDestroy');
    clearInterval(this.intervalId);
  }
}

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule, Child],
  template: `
    <h3>Lifecycle Hooks</h3>
    <button (click)="toggle()">Toggle Child</button>
    <child-cmp *ngIf="show"></child-cmp>
  `
})
export class App {
  show = true;
  toggle() { this.show = !this.show; }
}

bootstrapApplication(App);

                    
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Angular Lifecycle</title>
</head>
<body>
  <app-root></app-root>
</body>
</html>