Angular 2 在启动另一个函数之前等待一个函数到 return 一个值
Angular 2 waiting for a function to return a value before starting another function
我对 Angular 2.
还很陌生
我有 2 项服务,对于这个例子,我们说位置和价格。
我的第一个服务要求用户位置和 returns,货币代码和符号,基于他们的 IP 地址(不完美但对于这个例子它很好)以下得到 returned:-
{"code": "EUR","symbol": "€"}
当我尝试将值从 注入到另一个服务时,该服务 return 没有任何数据
private Prices(){
console.log(this.allPrices);
return this.allPrices;
}
this.locationService.getLocation().subscribe(
location => {this.currency = location},
error => console.log("Error: ", error),
() => {
console.log(this.currency),
this.priceApi.find({where: {currency: this.currency.code}}).map(this.Prices)
}
)
我是不是完全错了?
我将不胜感激,即使您为我指明了正确的方向,以便我能够自学。
如果您需要我post任何进一步的代码,请告诉我。
提前致谢!
@str 感谢您的反馈
我尝试了我在 上看到的内容
但它只是 console.logs 出来:-
Observable {_isScalar: false, _subscribe: function}
希望我答对了你的问题。这就是我通过链接 2 个调用来处理它的方法:
this.locationService.getLocation()
.flatMap(location => {
this.currency = location;
return this.priceApi.find({where: {currency: this.currency.code}})
}).map(prices => ....)
.subscribe(...)
我对 Angular 2.
还很陌生我有 2 项服务,对于这个例子,我们说位置和价格。 我的第一个服务要求用户位置和 returns,货币代码和符号,基于他们的 IP 地址(不完美但对于这个例子它很好)以下得到 returned:-
{"code": "EUR","symbol": "€"}
当我尝试将值从 注入到另一个服务时,该服务 return 没有任何数据
private Prices(){
console.log(this.allPrices);
return this.allPrices;
}
this.locationService.getLocation().subscribe(
location => {this.currency = location},
error => console.log("Error: ", error),
() => {
console.log(this.currency),
this.priceApi.find({where: {currency: this.currency.code}}).map(this.Prices)
}
)
我是不是完全错了?
我将不胜感激,即使您为我指明了正确的方向,以便我能够自学。
如果您需要我post任何进一步的代码,请告诉我。
提前致谢!
@str 感谢您的反馈
我尝试了我在
Observable {_isScalar: false, _subscribe: function}
希望我答对了你的问题。这就是我通过链接 2 个调用来处理它的方法:
this.locationService.getLocation()
.flatMap(location => {
this.currency = location;
return this.priceApi.find({where: {currency: this.currency.code}})
}).map(prices => ....)
.subscribe(...)