如何解决在ngOnInit中执行POST请求后,如何刷新HTML表?
访问组件invoices.ts
时,它将在后台查找一些更新并将其应用到现有更新。否则,当用户没有现有发票时,它将执行POST请求以创建它们。但是,当用户访问组件时,在ngOnInit()
中调用POST和PUT操作,直到刷新页面,我仍然看到一个空表。如何不使用`window.location.reload()'来自动刷新HTML表以填充新创建的发票?
invoice.component.ts
ngOnInit() {
this.auth.getProfile().subscribe((profile : any) => {
this.userName = profile.user.username;
this.email = profile.user.email;
}
this.getAndUpdateInvoices();
}
getAndUpdateInvoices() {
this.invoiceService.getInvoicesn().subscribe((data : any) => {
this.invoices= data.invoice;
this.createUpdate();
})
createUpdate() {
this.createInvoice({
client: data.Client,***etc
})
}
createInvoice(invoice) {
this.invoiceService.newInvoice(invoice).subscribe((data: any) => {
if (!data.success) {
console.log(data.message);
} else {
console.log(data.message);
}
});
}
this.invoiceService.updateInvoice(this.invoice).subscribe((data: any) => {
if (!data.success) {
console.log(data.message);
} else {
console.log(data.message);
}
});
}
在我的invoice.component.html
中:
<tr *ngFor="let invoice of invoices" >
<td> {{invoice.hours}} </td>
请让我知道是否需要更多信息
解决方法
只需在此处调用用于从服务类中获取所有数据的方法即可:
createInvoice(invoice) {
this.invoiceService.newInvoice(invoice).subscribe((data: any) => {
if (!data.success) {
console.log(data.message);
} else {
console.log(data.message);
-----HERE----
}
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。