Angular6 在角坐标上更新mat表
在数据库中插入数据后,我需要更新mat表。我的表单和mat表在同一页中。从表单中,我将数据插入数据库。在数据库上成功插入。但是我的mat表没有更新。如果我刷新页面,mat表将更新。但我需要在将数据插入数据库后更新mat表 春季服务 AppComponent.ts Appcomponent.htmlAngular6 在角坐标上更新mat表,angular6,Angular6,在数据库中插入数据后,我需要更新mat表。我的表单和mat表在同一页中。从表单中,我将数据插入数据库。在数据库上成功插入。但是我的mat表没有更新。如果我刷新页面,mat表将更新。但我需要在将数据插入数据库后更新mat表 春季服务 AppComponent.ts Appcomponent.html 您可以使用BehaviorSubject对象,通过该对象可以调用更新订阅元素的下一个函数,例如: @Component({selector:'my-component', templateUrl: '
您可以使用BehaviorSubject对象,通过该对象可以调用更新订阅元素的下一个函数,例如:
@Component({selector:'my-component', templateUrl: './my-component.html', style:[]})
export class MyComponentComponent implements OnInit, OnDestroy {
private dataObj = new BehaviorSubject<any>([]);
private dataSubscription: Subscription;
constructor(private http: HttpClient, springService: SpringService){
// Subscribe with BehaviorSubject
this.dataSunscription= this.dataObj
.asObservable()
.subscribe(res => this.clubdetails.data= res);
this.refreshData(); // fill with first data
}
ngOnInit(): void {}
ngOnDestroy(): void {
this.dataSubscription.unsubscribe();
}
// Refresh the data, and call the next function of BehaviorSubject
refreshData(){
this.springService.getAllClubs()
.toPromise()
.then( dt => this.dataObj.next(dt));
}
mymethod(){
// Call your code
refreshData(); // Refresh after post the data
}
}
您可以在上看到一个示例,您可以使用BehaviorSubject对象,通过此调用下一个函数来更新订阅元素,例如:
@Component({selector:'my-component', templateUrl: './my-component.html', style:[]})
export class MyComponentComponent implements OnInit, OnDestroy {
private dataObj = new BehaviorSubject<any>([]);
private dataSubscription: Subscription;
constructor(private http: HttpClient, springService: SpringService){
// Subscribe with BehaviorSubject
this.dataSunscription= this.dataObj
.asObservable()
.subscribe(res => this.clubdetails.data= res);
this.refreshData(); // fill with first data
}
ngOnInit(): void {}
ngOnDestroy(): void {
this.dataSubscription.unsubscribe();
}
// Refresh the data, and call the next function of BehaviorSubject
refreshData(){
this.springService.getAllClubs()
.toPromise()
.then( dt => this.dataObj.next(dt));
}
mymethod(){
// Call your code
refreshData(); // Refresh after post the data
}
}
您可以在app.component.ts中的上看到一个示例。请尝试以下代码
getClubList() {
this.springService.getAllClubs().pipe(
........
).subscribe(res => {this.clubdetails.data= res;
this.dataSource = new MatTableDataSource(this.Model_Obj_Name);});
}
在app.component.ts中,尝试以下代码:
getClubList() {
this.springService.getAllClubs().pipe(
........
).subscribe(res => {this.clubdetails.data= res;
this.dataSource = new MatTableDataSource(this.Model_Obj_Name);});
}
只需在getClubList方法中进行以下更改:
只需在getClubList方法中进行以下更改:
getClubList() {
this.springService.getAllClubs().pipe(
........
).subscribe(res => {this.clubdetails.data= res;
this.dataSource = new MatTableDataSource(this.Model_Obj_Name);});
}
getClubList() {
this.springService.getAllClubs().pipe(
........
).subscribe(res => {
this.clubdetails.data = res;
this.clubdetails.data = this.clubdetails.data;
});
}