我想返回一个 bool 值,但是“if”条件中的变量是未定义的。
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;
}
}
最佳答案
一旦您开始使用可观察对象,您就必须在可观察对象之外的链式方法集中工作。在您的情况下,您可以执行以下操作:
function InnerFunc(){
return this.menuDataService.getMenu()
.map(res => {
if(res.mainMenus == 1){
return true;
}
else{
return false;
}
)
}
function OuterFunc() {
InnerFunc()
.subscribe(result =>{
// will be either true or false
console.log(result);
});
}
关于angular - 以 Angular 订阅后如何执行某些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44950436/