无法将动态数据绑定到Angular Material选择表单中

时间:2019-12-16 05:52:32

标签: angular angular-material

我无法使用Angular8的Angular Material选择表单动态显示数据。我的代码在下面。

product.componet.html:

<form [formGroup]="productForm" (submit)="saveProduct($event)" novalidate> 

    <mat-form-field appearance="outline" class="col-lg-4 col-md-6 col-sm-12">
        <mat-label>Product Name</mat-label>
           <input matInput placeholder="Product Name" aria-label="Product Name" formControlName="Name" maxlength="70" required>
    </mat-form-field>
   <mat-form-field appearance="outline" class="col-lg-4 col-md-6 col-sm-12">
                            <mat-label>Gender</mat-label>
                            <mat-select
                                placeholder="Select Gender" 
                                aria-label="Select Gender" 
                                (selectionChange)="onGenderSelectionChanged($event)"
                            >
                                <mat-option *ngFor="let s of genders" [value]="s.Code">
                                    <span *ngIf="!s.Code">
                                    </span>
                                    <span *ngIf="s.Code">
                                        {{s.Name}}
                                    </span>
                                </mat-option>
                            </mat-select>
                        </mat-form-field>

</form>

Product.componet.ts:

genders: any = [
    '',
    {
      Code: "M",
      Name: "Male"
    },
    {
      Code: "F",
      Name: "Female"
    },
    {
      Code: "U",
      Name: "Unisex"
    }
  ];
this.productForm.setValue({
     Name : String = 'Test product',
     Gender: String = 'M(Male)'
})

我能够显示产品名称,但无法显示动态绑定的下拉值。我需要使用Angular 8动态显示下拉值。

2 个答案:

答案 0 :(得分:3)

在发布的代码中有几件事是错误的:

  1. 您必须将formControlName控件的性别控件使用<mat-select>

例如:

                Here
            \/\/\/\/\/\/
<mat-select formControlName="Gender" placeholder="Select Gender" aria-label="Select Gender" (selectionChange)="onGenderSelectionChanged($event)">
  <mat-option *ngFor="let s of genders" [value]="s.Code">
     <span *ngIf="!s.Code"></span>
     <span *ngIf="s.Code"> {{s.Name}}</span>
  </mat-option>
</mat-select>
  1. 虽然FormGroup的设置值不需要指定数据类型,并且
this.productForm.setValue({
  Name : 'Test product',
  Gender: 'M'
});  

不是M(Male),这是完全不同的。来自M,用于绑定matselect的值。

Working_Demo

答案 1 :(得分:0)

代替检查,尝试使用将值设置为“”

<span>                                        
   {{s.Name}}                                    
</span>

您的数组为

genders: any = [
    {
      Code: "",
      Name: ""
    },
    {
      Code: "M",
      Name: "Male"
    },
    {
      Code: "F",
      Name: "Female"
    },
    {
      Code: "U",
      Name: "Unisex"
    }
  ];

设置垫选择值

<mat-select placeholder="Select Gender" aria-label="Select Gender"  (selectionChange)="onGenderSelectionChanged($event)" formControlName="Gender">
 <mat-option *ngFor="let s of genders" [value]="s.Code">
     <span *ngIf="!s.Code"></span>
     <span *ngIf="s.Code">
       {{s.Name}}
     </span>
 </mat-option>
</mat-select>

将formControlName添加到mat select标签

formControlName="Gender"