I'm wondering how can I pass a value from ngOnInit to function at the same component.
Here is my example. I get data from API, to display context of my page. Later when I click the button I would like to use that data, but in console.log I get undefined. How's that undefined since data was already loaded and displayed on my page.
...
clientId1;
clientId2;
clientNote;
errorMsg;
ngOnInit() {
this.clientId1 = this.route.parent.snapshot.paramMap.get('userId1');
this.clientId2 = this.getMethodClientId2();
}
getMethodClientId2(): void {
this.userService.getUser().subscribe(
(data) => {
this.clientNote = data.ClientNotes;
},
(error) => (this.errorMsg = error)
);
}
postMethodOnClick() {
console.log(this.clientId1);
console.log(this.clientId2);
}
...