【问题标题】:How to group several checkboxes with angular-material如何使用角度材料对多个复选框进行分组
【发布时间】:2018-06-06 21:56:12
【问题描述】:

假设我有一个创建商店的表单。我想输入它的名字,以及开店的日子。

所以我将有一个带有一些输入的表单,并且我想将复选框分组到一个 mat-form-field 中。

store-form-component.html :

<form (ngSubmit)="onSaveStore()" [formGroup]="storeForm">
  <mat-form-field>
    <mat-label>Store name</mat-label>
    <input matInput placeholder="store name" formControlName="name" required>
  </mat-form-field>
  <mat-form-field [formGroup]="storeForm.controls.availableDays>
    <mat-checkbox *ngFor="let availableDay of storeForm.controls.availableDays.controls; let i=index" formControlName="{{i}}">{{i}}</mat-checkbox>
  </mat-form-field >
</form>

store-form-component.ts :

export class StoreFormComponent implements OnInit {

  // Form Groups
  storeForm: FormGroup;

  constructor(
    private formBuilder: FormBuilder
  ) {}

  ngOnInit(): void { 
    this.initForm();
  }

  initForm(): void {
    this.storeForm = this.formBuilder.group({
      name: "",
      availableDays: this.getAvailableDays()
    });
  }

  getAvailableDays(): FormGroup {
    return this.formBuilder.group({
      monday: false,
      tuesday: false,
      wednesday: false,
      thursday: false,
      friday: false,
      saturday: false,
      sunday: false
    });
  }

它不起作用,我不知道为什么......

编辑:感谢@g-tranter(和其他 SO 帖子),我可以解决这个问题。最终代码如下:

store-form-component.html :

<form (ngSubmit)="onSaveStore()" [formGroup]="storeForm">
  <div [formGroup]="storeForm.controls.availableDays">
    <mat-checkbox *ngFor="let availableDay of days;" formControlName="{{availableDay}}">{{availableDay}}</mat-checkbox>
  </div>
</form>

store-form-component.ts :

export class StoreComponent implements OnInit {
  // Form Groups
  storeForm: FormGroup;
  days: Array<string>;

  constructor(private formBuilder: FormBuilder) {}

  ngOnInit(): void {
    this.initForm();

    // this is useful to iterate over the form group
    this.days = Object.keys(this.storeForm.controls.availableDays.value);
  }

  initForm(): void {
    this.storeForm = this.formBuilder.group({
      name: "",
      availableDays: this.getAvailableDays()
    });
  }

  getAvailableDays(): FormGroup {
    return this.formBuilder.group({
      monday: false,
      tuesday: false,
      wednesday: false,
      thursday: false,
      friday: false,
      saturday: false,
      sunday: false
    });
  }
}

【问题讨论】:

    标签: javascript angular angular-material


    【解决方案1】:

    也许你需要一个垫子选择列表

    查看Angular Material documentation

    希望对你有帮助!

    【讨论】:

    • 不幸的是,我无法将此 mat-selection-list 绑定到表单。但是@g-tranter 给了我一个很好的提示来解决我的问题。
    【解决方案2】:

    您正在将表单控件名称设置为数字索引:

    formControlName="{{i}}"
    

    表单组中不存在。

    您需要将其设置为“星期一”等或设置

    [formControl]="availableDay"
    

    【讨论】:

    • 谢谢,它帮助我解决了我的问题。我想到了其他问题(因为它是一个对象,所以无法遍历 formGroup)。我将编辑我的第一篇文章以放置最终代码。
    猜你喜欢
    • 2019-06-02
    • 2019-09-11
    • 2016-01-23
    • 1970-01-01
    • 2015-05-18
    • 2019-01-02
    • 2019-04-16
    • 2023-03-09
    • 2021-05-15
    相关资源
    最近更新 更多