Reputation: 75
I have created one component for testing purposes to check the ngOnChanges()
fire or not.
As I learn that the ngOnChanges()
is the kick start event that fires after the Constructor()
gets executed, but in my application it not working.
My TS Code
constructor() {
console.log("constructor");
}
ngOnChanges(){ console.log("ngOnChanges"); }
ngOnInit(){
console.log("ngOnInit");
}
ngOnDestroy(){
console.log("ngOnDestroy");
}
ngDoCheck(){
console.log("ngDoCheck");
}
ngAfterContentInit(){ console.log("ngAfterContentInit"); }
ngAfterContentChecked(){ console.log("ngAfterContentChecked"); }
ngAfterViewInit(){ console.log("ngAfterViewInit"); }
ngAfterViewChecked(){ console.log("ngAfterViewChecked"); }
HTML PAGE CODE
<h2>Test My Angular Life Cycle</h2>
<input type="buttton" value=""/>
Upvotes: 0
Views: 525
Reputation: 1347
Does your component implements life cycle interfaces?
I mean
export MyComponent implements OnInit, OnDestroy..{
...
}
Upvotes: 0
Reputation: 31
You can watch only detections triggered by change from component outside.
Working example: https://stackblitz.com/edit/angular-ivy-pzsl81
Upvotes: 0
Reputation: 597
ngOnChanges will call before ngOnInit() only if the component has bound inputs and whenever one or more data-bound input properties change. if your component has no inputs or you use it without providing any inputs, the framework will not call ngOnChanges().
Upvotes: 2