首页 > 解决方案 > POST请求后如何实时刷新数据?

问题描述

我有一个显示数据的表格。一切都适用于POSTGET请求,但只有在刷新页面时我才能看到插入后的值。

abc.service.ts

createExamCategory(options) {
    return this.http.post<{ message: string }>(this.url + '/createExamCategory', options);
}

getExamCategory():Observable<any> {
    return this.http.get<any>(this.url + '/getAllExamCategory');
}

abc.component.ts

onSubmit() {
    if(this.formdata.invalid) {
      return;
    }
    this.adminCategory.createExamCategory(this.formdata.value).subscribe(reponse => {
      console.log(reponse.message);
    }); 
}

ngOnInit() {
  this.columnsToDisplay = ['id', 'examCategoryName', 'isActive', 'Action'];
  this.adminCategory.getExamCategory().subscribe((reponse: any) => {
      this.ExamCategoryData = reponse.examCategoryList;
      this.dataSource = new MatTableDataSource(this.ExamCategoryData);
      this.dataSource.paginator = this.paginator;
  }, error => {
       console.log(error);
     }
  );
}

这是我的代码。我需要做哪些必要的改变?

标签: javascriptangularangular9

解决方案


您在这里有两个选择:

  1. 将您发送到 POST 请求的数据直接添加到表中。

  2. POST 刷新表后获取整个数据集。


推荐阅读