Angular 5从http post返回对象
我正在努力将json从服务层传递到组件,然后依次调用函数。以下是我已经完成的工作:Angular 5从http post返回对象,angular,typescript,Angular,Typescript,我正在努力将json从服务层传递到组件,然后依次调用函数。以下是我已经完成的工作: users.service.ts addUser(user: User) { return this.http.post('http://localhost:8080/user/create', user).map(res => res.json()); } 然后是组件层: async onSubmit(model) { await new Promise(resolve => {
users.service.ts
addUser(user: User) {
return this.http.post('http://localhost:8080/user/create', user).map(res => res.json());
}
然后是组件层:
async onSubmit(model) {
await new Promise(resolve => {
setTimeout(() => {
this.submitted = true;
this.usersService.addUser(model).subscribe(data => this.savedModel = data);
resolve();
}, 500);
});
this.sendNotification();
}
@Output() notifyParent: EventEmitter<any> = new EventEmitter();
sendNotification() {
this.notifyParent.emit(JSON.stringify(this.savedModel));
}
看起来,
addUser
可能需要500秒以上的时间,但不是创建一个可以在用户服务完成时发出的人工计时器
onSubmit(modal) {
this.usersService.addUser(model).subscribe(data => {
this.savedModel = data;
this.sendNotification(); // or just .emit here
});
}
你也不需要用承诺或任何东西来包装它。你在组件中声明了
savedModel
吗?你的父组件看起来怎么样,(与此相关的子标记和TS代码)我不明白。我按照你的建议做了,但仍然没有定义。
onSubmit(modal) {
this.usersService.addUser(model).subscribe(data => {
this.savedModel = data;
this.sendNotification(); // or just .emit here
});
}