In this simple component with only one input box and two-way binding I wanted to test OnChanges life cycle hook by logging to console when I type into input. ngOnChanges is not logging to console. ngOnInit is. What is the best way to make it work?
import { Component, SimpleChange } from '@angular/core';
@Component({
template: `
<div>
hello life
<input type="text" placeholder="hassan" [(ngModel)]="name" />
<p>my name is:{{ name }}</p>
</div>
`,
selector: 'hook-life',
})
export class LifeCycleComponent {
name: string = '';
constructor() {
console.log('constructor ');
}
ngOnChanges(c: SimpleChange) {
console.log(`onchanges ${c.currentValue} `);
}
ngOnInit() {
console.log('onInit');
}
ngOnDestroy() {
console.log('onDestroy');
}
}
OnInit,OnChange,OnDestroyinterfaces. All you've done is defined the methods but you've not implemented the interfaces. - Roddy of the Frozen Peas