Issue
I need to set myVariable value indicated inside the function load(), where I collect a data from the service and show it in another function (otrafuncion()), but I don't know the syntax.
export class clase{
public this.miVariable:any;
constructor(){}
ngOnload():void{
this.load();
this.otraFuncion();
}
load() {
this.loadService.cargar().subscribe(
(respuesta) => {
this.carga = respuesta;
this.miVariable=this.carga.mivalorRecogido; //necesito este valor
}
);
}
}
otrafuncion(){
console.log(this.miVariable);
}
}
Solution
if I can suggest a solution, your variable that you're interested in should be an observable, since it's only available asynchronously.
export class clase{
public carga$ = this.loadService.cargar().pipe(shareReplay(1))
public miVariable$ = this.carga$.pipe(map(c => c.mivalorRecogido));
}
now when you're interested in either of these properties, you just subscribe to them. the shareReplay
operator will ensure your value is cached and only requested once, and will only be requested when it is first needed.
Answered By - bryan60
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.