The official Angular docs says:
"ngOnChanges is a lifecycle hook that is called when any data-bound property of a directive changes. Define an ngOnChanges() method to handle the changes.
It is a little bit unclear to mean that if the updated/latest input property actually available in ngOnChanges lifecycle method. So I did a simple experiment as:
//trmplate.html
<p [mydirective] = "GetFirstStudent().Name"></p>
where Name is a property in the component, and I have set the first person's name to be "Michael".
so in the directive class that as mydirective selector:
@Directive({
selector: "[mydirective]"
})
export class CustomDirective{
...
@Input("mydirective")
personName: string;
ngOnChanges() {
console.log(personName);
}
}
so I refresh the browser, and the console does have the following output:
Michael
So this confirmed that in ngOnChanges, I can access the latest input properties.
Is my understanding correct?
ngOnChangesworks? If so, the answer is yes. - Vlad VidacngOnChanges(changes : SimpleChanges).changes.personName.currentValuewill store the current value - CruelEngineMichael, so I'm not sure what you're looking for here. - Jesse