I want to return a boolean value ,but the variable in the "if" condition is undefined.
function() {
this.menuDataService.getMenu()
.subscribe(res => {
this.mainMenus = res.MainMenus;
console.log(this.mainMenus);
});
console.log(this.mainMenus);
if(this.mainMenus == 1){
return true;
}
else {
return false;
}
}
An Observable instance begins publishing values only when someone subscribes to it. You subscribe by calling the subscribe() method of the instance, passing an observer object to receive the notifications. Returns an Observable instance that synchronously delivers the values provided as arguments.
Normally Subscription means an arrangement to receive something. Similarly, in Angular applications Observables will be connected to observers and whenever they observe a new value or change in data, they will execute code with the help of Subscription and all the subscribed components will receive the updated outcome.
seescode's answer helped me but I can't comment it.
I just wanted to say that with RxJS 6, their way to chain Observable operators changed, and we now have to use the pipe(...) method. (see RxJS 6 - What Changed? What's New?)
Here is what his InnerFunc would look like updated to RxJS 6 :
function InnerFunc(){
return this.menuDataService.getMenu().pipe(
map(res => {
if(res.mainMenus == 1){
return true;
}
else{
return false;
}
)
)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With