Javascript 仅在从POST获得响应后获取变量
只有在收到POST请求的响应后,我才能在组件中设置变量 component.ts代码:Javascript 仅在从POST获得响应后获取变量,javascript,angular,typescript,ionic-framework,Javascript,Angular,Typescript,Ionic Framework,只有在收到POST请求的响应后,我才能在组件中设置变量 component.ts代码: formSubmit() { this.sent = this.submitProvider.sendByPost(this.form); this.formSent = this.submitProvider.formSent; } 服务提供商/提供商.ts: sendByPost(form) { return this.http.post("http://app.api.com/
formSubmit() {
this.sent = this.submitProvider.sendByPost(this.form);
this.formSent = this.submitProvider.formSent;
}
服务提供商/提供商.ts:
sendByPost(form) {
return this.http.post("http://app.api.com/mail/", form, httpOptions)
.subscribe(
data => (
this.formSent = true,
), // success path
error => (console.log(error)), // error path,
() => this.formSent = true
)
}
嗯,也许你会做这个 你的服务
sendByPost(form) {
return this.http.post("http://app.api.com/mail/", form, httpOptions).toPromise() // you don't need observable here
}
然后将其订阅到组件中
this.submitProvider.sendByPost(this.form)。然后(res=>this.formSent=true)
或者保持你的可观察性,并做同样的事情
sendByPost(form) {
return this.http.post("http://app.api.com/mail/", form, httpOptions) // you don't need observable here
}
组件将是
this.submitProvider.sendByPost(this.form).subscribe(res => this.formSent = true)
移动订阅组件,如此处文档所示: