0
How do you take this value of the user variable and put it inside the Formgroup? No . ts I did so:
ngOnInit(): void {
this.id = this.activatedRoute.snapshot.params['id'];
this.service.listById(this.id).subscribe(obj => this.user = obj);
//alert("Nome: " + this.user.name); // undefined!!
this.formEdit = this.formBuilder.group({
name: ['',[Validators.required, Validators.minLength(2), Validators.maxLength(256)]],
imageUrl: ['',[Validators.minLength(10)]],
email: ['',[Validators.required, Validators.minLength(8), Validators.maxLength(256)]],
page: ['',[Validators.minLength(8)]],
registrationDate: [this.date],
});
}
The id is coming quietly. With interpolation I can print a "user.name", but if I give an alert with "user.name" inside . ts, it gives Undefined.
Just me on this same site, I have commented 500 times this same question. The method
listById(this.id)
is a Observable which would be equivalent toPromise
of Javascript, and the subscribe would be equivalent to then, in short, they are all methodsassíncronos
, i.e., the response of the server to which the request was made can be fast, linger or not happen. So when you give Alert() the variable isundefined
, pq it has not yet been defined (filled with the API response)!– LeAndrade
Impressive how every 3 questions of one is that, it seems that the site is not working pro people search
– Eduardo Vargas
It does, but my case here was only to know how to write what I want, in a way that the angular understands.
– Aluizio Júnior