'How can I get value from subscribe? [duplicate]
I tried to do this, but it doesn't work. Any help is appreciated thanks!
export class OuterClass {
let isEffectiveUrl = (url:string) => {
let tmpRes:boolean
this.http.get(url).subscribe((result) => {
tmpRes = Object.keys(result).length > 0;
});
return tmpRes;
}
}
Solution 1:[1]
You're setting isEffectiveUrl to be a function, not the resulting evaluation. One way to do this would create a local variable and then execute your http.get within a function or one of your lifecycle hooks.
e.g.
export class OuterClass {
isEffectiveUrl: boolean = false;
testUrl(url: string): void {
this.http.get(url).subscribe((result) => {
this.isEffectiveUrl = Object.keys(result).length > 0;
});
}
}
Just remember, this is a reactive model. So what's being run in the subscribe block is asynchronous.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mfaith |
