如何更新字段从post方法在角?



我想通过点击从matdialog提交来更新字段。

html

<h2>network <mat-icon (click)="openDialog(mytemplate)">add_circle_outline</mat-icon></h2>
<ng-template #mytemplate>
<div class="container">
<div class="box">

<mat-form-field appearance="outline">
<input matInput placeholder="enter the items" />
</mat-form-field>
<button mat-stroked-button mat-dialog-close color="accent">CANCEL</button>
<button mat-flat-button color="accent" (click)="onclick()">Submit</button>
</div>
</div>
</ng-template>

.ts

provider = [];

providers: provider;
getnetwork() {
this.service.getAllNetworkProviders().subscribe(res => {
this.provider = res.data;
this.dataSource = this.provider;
});
}

openDialog(template): void {
// ask user to confirm, if he really wants to proceed
this.dialogRef = this.dialog.open(template);
this.dialogRef.afterClosed().subscribe(isTrue => {
if (isTrue) {
const activatenetworkprovider = { networkName: this.providers.id };
this.service.networkProviderStatus(activatenetworkprovider).subscribe(data => {
this.getnetwork();
this.snackBar.open('Successfully created new network provider  sim', 'Close', { duration: 2000 });
});
}
});
}

model.ts

export interface provider {
id: number;
name: string;
}

我得到错误[error TypeError: Cannot read properties of undefined (reading 'id')]] .

我已经更新了问题。

声明:

providers: provider;

它不是一个实际的对象/实例,而只是一个对象的句柄。你仍然需要给=赋值。最好是包含成员id的对象(例如:{id: 1,…}) .

用大写字母命名接口也是惯例,像这样:

export interface Provider {
id: number;
name: string;
}

相关内容