【发布时间】:2020-11-18 04:28:00
【问题描述】:
您好,我尝试为我的 Datatable 设置选项,然后在我的对象中添加一个新字段,但我需要等待这些 dtOptions 完成,我该如何等待 ngOnInit 中的 this.dtoptions
export class MyDashboardComponent implements OnInit {
grades: any = [];
dtOptions: DataTables.Settings = {};
buffer: any = "";
constructor(private apiService: ApiService, private _auth: AuthService, private http: HttpClient) {
}
async ngOnInit(): Promise<void> {
this.dtOptions = {
order: [[0, "desc"]],
responsive: true,
pagingType: 'full_numbers',
pageLength: 2,
serverSide: true,
processing: true,
ajax: (dataTablesParameters: any, callback) => {
this.http
.post<DataTablesResponse>('http://localhost:4000/grade/readByStudentId', {
login: this._auth.getUserData().login,
dataTablesParameters
}).subscribe(resp => {
this.grades = resp.data;
callback({
recordsTotal: resp.recordsTotal,
recordsFiltered: resp.recordsFiltered,
data: []
});
});
},
columns: [{data: 'id'}, {data: 'firstName'}, {data: 'lastName'}]
};
await this.grades.forEach(grade => {
this.getTpName(grade.tp_id)
.pipe(take(1))
.subscribe(tpName => {
grade.push({tpName : tpName});
});
});
}
【问题讨论】:
标签: angular typescript async-await