我在组件中有一个按钮。当我单击此按钮时,它将打开MatDialog
。该对话框包含许多货币,这些货币是我从特殊api获取的,但这些货币未出现在模式中。
这是我的代码:
displayedColumns:string[] =
'CRCYIsn','CRCYDesc','CRCYConversionUnitsCount'];
currencyAndConversion: Currency[];
constructor(
private service :InvoiceService,
private fb: FormBuilder,
private dialogRef: MatDialogRef<CurrencyComponent>,
private http:HttpClient
) { }
ngOnInit() {
this.service.getCurrencyList()
.then(res => this.currencyAndConversion = res as Currency[]);
}
dataSource = new MatTableDataSource(this.currencyAndConversion);
但是当这样传递数据时,它会起作用
currencyAndConversion1=[
{
"CRCYIsn": 100000,
"CRCYArabicName": "جنيه استرلينــي",
"ConvPrice":222
},
{
"CRCYIsn": 25896354,
"CRCYArabicName": "جنيه مصري",
"ConvPrice":222
}
{
"CRCYIsn": 89945454545,
"CRCYArabicName": "درهم اماراتى",
"ConvPrice":222
}]
根据JB Nizet的评论,我在this.currencyAndConversion初始化之前创建了数据源。初始化后对其进行初始化,即在传递给then()的回调内部。那么,在这种情况下(在大多数情况下),其含义是“并且,稍后,当对HTTP请求的响应最终返回时,异步地执行以下代码”
编辑:我用此命令修改了ngOnInit()中的代码
dataSource: MatTableDataSource<Currency>;
ngOnInit() {
this.service.getCurrencyList().then(res =>
{
this.currencyAndConversion=res as Currency[];
console.log(res);
this.dataSource = new MatTableDataSource(this.currencyAndConversion);
}
)}