0

Inside the typescript, there are a name variable takes input from another component.

@Input() name : any ; 

And inside the ngOnChange I print the SimpleChange object like this.

ngOnChanges(changes: SimpleChange){
    console.log(changes);
    console.log(changes.currentValue);
}

The result in the console is as followed. enter image description here

I don't understand why in console, it can tells there are string "Cafeteria Old Beach" for currentValue property. Yet when I console.log(changes.currentValue), it returns undefined. How can I properly access the currentValue property value? Thanks in advance.

AkiZukiLenn
  • 337
  • 3
  • 14

1 Answers1

1

it is because after the changes there should be prop, in your case you have made changes.currentValue. and also change SimpleChange to SimpleChanges Please try following.

import { SimpleChanges } from '@angular/core';
export class TestComponent {
 @Input() name : any ;
 
constructor(){}

ngOnChanges(changes: SimpleChanges){
    console.log(changes);
    console.log(changes.name.currentValue);
    console.log(changes.name.previousValue);

}

}
  • It throws error ... error TS2339: Property 'name' does not exist on type 'SimpleChange'. / error TS2304: Cannot find name 'SimpleChanges'. – AkiZukiLenn Aug 02 '22 at 20:19
  • 1
    Yeah it works now , Turns out I haven't "update angular core" , with the help of visual studio code quick fix option , SimpleChanges works and its not undefined anymore . – AkiZukiLenn Aug 02 '22 at 20:22