You cannot prevent this behavior, but you can:
Use the topic:
class Foo implements OnChanges,OnInit{
onChanges = new Subject<SimpleChanges>();
ngOnInit(){
this.onChanges.subscribe((data:SimpleChanges)=>{
});
}
ngOnChanges(changes:SimpleChanges){
this.onChanges.next(changes);
}
}
Use the boolean property:
class Foo implements OnChanges,OnInit{
initialized=false;
ngOnInit(){
this.initialized = true;
}
ngOnChanges(changes:SimpleChanges){
if(this.initialized){
}
}
}
Use API SimpleChanges
You can also check the method : SimpleChange.isFirstChange()
isFirstChange(): boolean Check if the new value is the first assigned value.
class Foo implements OnChanges,OnInit{
@Input()
bar:any;
ngOnInit(){
}
ngOnChanges(changes:SimpleChanges){
if(!changes["bar"].isFirstChange()){
}
}
}
source
share