gpt4 book ai didi

javascript - Angular 2 http.get订阅: How to call another function when service completed?

转载 作者:行者123 更新时间:2023-11-28 13:10:57 26 4
gpt4 key购买 nike

没明白。例如,如果我需要我的结果做更多​​事情,而不仅仅是输入我的变量英雄。我想在成功或完成时调用另一个函数,但我不能。为什么会这样以及应该如何做?我有另一个变量需要获取从响应返回的相同数据(它的副本),但我只能在获取数据后才能制作副本。

this.myService.getHeroes()
.subscribe(
function(response) {
response => this.heros = response;
},
function(error) {
console.log("Error happened" + error)
},
function() {
console.log("the subscription is completed");
}
);

最佳答案

您可以在收到响应后立即调用该函数。

this.myService.getHeroes()
.subscribe(res => {
this.heros = res;
//insert whatever you want here, e.g. function which needs to wait for asynchro response
},
error => {
console.log("Error happened" + error)
}
);

关于javascript - Angular 2 http.get订阅: How to call another function when service completed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42448468/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com