我希望在邮件请求成功结束后调用函数。如何实现?
这是我的帖子请求:
this.http.post(this.myUrl, formData, options)
.map(res => res.json())
.catch(error => Observable.throw(error))
.subscribe(
data => console.log('success'),
error => console.log(error)
)
答案 0 :(得分:0)
在回调中使用{}
,您可以根据需要添加尽可能多的代码。
this.http.post(this.myUrl, formData, options)
.map(res => res.json())
.catch(error => Observable.throw(error))
.subscribe(
data => {
// you can do everything you want here
console.log('success');
this.myCustomFunction();
},
error => console.log(error)
)
答案 1 :(得分:0)
this.http.post(this.myUrl, formData, options)
.map(res => res.json())
.subscribe(
(data) => {
console.log('success');
this.myFunction(); //call any function
},
(error) => {
console.log(error)
});
http.post(...)。map(...)返回一个Observable,.subscribe(...)它是一个异步的进程,如果你想在http响应后调用一个函数你必须在里面做在函数中订阅
(data) => { // inside invoke the function that you want}