4

I am passing a Map to a child component but when I set new key-value pair child component cannot detect it in ngOnChange but the first time it boot only.

Is it a bug or simply not supported?

tom10271
  • 4,222
  • 5
  • 33
  • 62

1 Answers1

2

ngOnChanges will not be called when there are internal mutations in the input data.

https://vsavkin.com/immutability-vs-encapsulation-90549ab74487

What I usually do is to break the reference to the variable. For an array I use map().

let array:number[] = [1, 2, 3];
let newArray:number[] = array.map(num => num);

You can also look at the OnPush change detection strategy.

gyc
  • 4,300
  • 5
  • 32
  • 54