While working with @Input() data coming from the parent component, I am utilizing ngOnChanges to detect any changes. However, it seems that the method only triggers once. Even though the current value is updated, the previous value remains undefined. Below is an outline of my approach:
myArray=[];
ngOnChanges(changes:SimpleChanges):void{
console.log(changes);
if(changes.myInput.currentValue != undefined && changes.myInput.currentValue != changes.myInput.previousValue){
for(var i=0;i<this.changes.myInput.currentValue.length;i++){
console.log("length is" , i);
this.myArray.push(this.changes.myInput.currentValue);
}
}
}
The issue here is that console.log(changes)
is only executed once. Despite the fact that the content within it updates whenever @Input()
changes. The myInput.currentValue
variable holds an array, and my intention is to perform a certain action every time a new object is added to the array through @Input()
from the parent component. Unfortunately, it never enters the if condition, and the length cannot be determined.