0

I am using ngOnChanges and I've set the SimpleChange parameter as below. Why is it that I can directly access my input property using changes.currentValue without having to get the property using indexing? Both work and I'm confused as to why?

  ngOnChanges(changes: SimpleChange) {
    console.log('ngOnChanges called with changes value: ', changes.currentValue) // why does this work? is it because by convention it works if I have just one input property??
   
    for (const propName in changes) {
      console.log('ngOnChanges called with propName value: ', changes[propName].currentValue)
    }
  }
mo_maat
  • 2,110
  • 12
  • 44
  • 72

1 Answers1

1

Your declaring the changes parameter with the SimpleChange type but according to the docs they should be of type SimpleChanges(note the 's' at the end);

ngOnChanges(changes: SimpleChanges): void {
    console.log(changes['YOUR_PROP'].currentValue);
}
eroironico
  • 1,159
  • 1
  • 15
  • Aaaa! That's a typo, but then that makes me wonder why am still able to use the for loop on the changes param if it is just a single object? I still get the full object and can access it's keys when I do changes[propName] inside the for loop. – mo_maat Dec 14 '21 at 17:40