[英]How to store and pass the value of Mat-Select using ngModel
我有一个带有两个选项的mat-select,即用户可以选择他是个人客户还是组织客户。 现在我想将选择的值传递给服务类。 我怎样才能做到这一点。 我有一个表单,在用户选择一个Option后显示。填写表单后,用户点击保存并保存表单的所有信息。 有了这些信息,我也希望能够保存mat-select选项。 我怎样才能做到这一点。
<!-- Dropdown to Select Type of Customer -->
<mat-form-field>
<mat-label>Select Customer Type</mat-label>
<mat-select (onSelectionChange)="getCustType($event)">
<mat-option *ngFor="let obj of custType" (click)="getCustType(obj)"
[value]="obj.value"> {{ obj.viewValue }}</mat-option>
</mat-select>
</mat-form-field>
打字稿代码
custType: any[] = [{ value: 'individual', viewValue: 'Individual Customer' }, { value: 'organizational', viewValue: 'Organizational Customer' }];
要求保存按钮单击的要求函数
saveIndCustData() {
const savedIndCustomer = {
agreementId: this.agreementId,
prefix: this.prefix,
nameType: this.indCustNameType,
firstName: this.firstName,
middleNAme: this.middleName,
lastName: this.lastName,
gender: this.gender,
dateOfBirth: this.parseDate(this.dateOfBirth.toString()),
citizenship: this.citizenship
};
this.savedIndCustomer.emit(savedIndCustomer);
}
我想在上面的表单中传递mat-select的值我该怎么做?
将[(ngModel)]="this.selectedCustomerType"
到mat-select
标签,例如:
<mat-select [(ngModel)]="this.selectedCustomerType" (onSelectionChange)="getCustType($event)">
然后在* .component.ts文件中声明公共变量selectedCustomerType
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.