import { bootstrapApplication } from '@angular/platform-browser';
import { Component, Input } from '@angular/core';
@Component({
selector: 'child-cmp',
standalone: true,
template: `
<p>Child received: {{ text }}</p>
<p>Last change: {{ lastPrev || '∅' }} -> {{ lastCurr || '∅' }}</p>
`
})
export class Child {
@Input() text = '';
lastPrev = null;
lastCurr = null;
ngOnChanges(changes) {
const c = changes['text'];
if (c) {
this.lastPrev = c.previousValue ?? null;
this.lastCurr = c.currentValue ?? null;
}
}
}
@Component({
selector: 'app-root',
standalone: true,
imports: [Child],
template: `
<h3>OnChanges</h3>
<label>Text: <input #i (input)="text=i.value" placeholder="Type here..."></label>
<child-cmp [text]="text"></child-cmp>
`
})
export class App {
text = '';
}
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 - OnChanges</title>
</head>
<body>
<app-root></app-root>
</body>
</html>