After obtaining JSON data from the backend, my goal is to store it in a variable for populating a table. Below is the code snippet I am working with:
@Component({
selector: 'app-kontakte',
templateUrl: './kontakte.component.html',
styleUrls: ['./kontakte.component.scss'],
})
export class KontakteComponent implements OnInit {
textArea: string;
minlength: number;
maxlength = 100000;
constructor(private kontakteService: KontakteService) {
this.textArea = '';
this.minlength = 1;
}
ngOnInit(): void {
this.reload();
}
public reload():void {
this.kontakteService.getTpoContacts().subscribe((res) => {
this.textArea = JSON.stringify(res);
console.log(this.textArea); // <-- Data output is visible here
});
console.log(this.textArea) // <- Empty string appears here
}
}
My intention was to store the JSON data in the textArea variable and then utilize it to populate a table. The retrieved data inside the subscription functions properly, but outside of it, the this.textArea remains empty.
If you have any suggestions on how I can resolve this issue, please share as I am relatively new to Angular.