如何以垫选项角度显示数据?

时间:2020-02-24 07:28:17

标签: angular angular-material

我有一个对象,我想在表单字段中显示它,所有字段均在单击按钮而不是选择选项后填充。出了什么问题。

请帮助我找到错误。下面是我的代码。

对象

{
   id: 12
   category_name: "category 1"
   author_id: 5
   customer_id: 12
}

打字稿

this.form = this.fb.group({
   id: [null],
   category_name: [null, Validators.required],
   author_id: [this.auth.loggedInUserId.id],
   customer_id: [null]
})

editCategory(data) {
  this.form.patchValue(data);
  console.log(this.form.value);
}

HTML

<mat-form-field>
   <mat-label>Select Customer</mat-label>
   <mat-select [formControl]="form.controls.customer_id">
      <mat-option></mat-option>
      <mat-option *ngFor="let customer of customers" value="{{customer.id}}">{{customer.firstname}}
         {{customer.lastname}}
      </mat-option>
   </mat-select>
</mat-form-field>

2 个答案:

答案 0 :(得分:0)

只需尝试一下。我希望这会很好。

<mat-option *ngFor="let customer of customers" [value]="customer.id"> 
    {{customer.firstname}{{customer.lastname}}
 </mat-option>

答案 1 :(得分:0)

打字稿

this.form = this.fb.group({
   id: new FormControl(0),
   category_name: new FormControl('',Validators.required),
   author_id: new FormControl(this.auth.loggedInUserId.id),
   customer_id: new FormControl(0)
})

HTML

<mat-form-field>
    <mat-select placeholder="Select Customer" formControlName="customer_id" required>
    <mat-option *ngFor="let customer of customers" [value]="customer.id">{{customer.firstname}} {{customer.lastname}}</mat-option>
    </mat-select>
</mat-form-field>