admin管理员组文章数量:1221301
Let's say I have a form to create a store. I want to input its name, and the days the store will be open.
So I'll have a form, with some inputs, and I want to group checkboxes in one 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
});
}
It does not work and I can't figure why...
edit : thanks to @g-tranter (and others SO posts), I could fix the issue. The final code looks like :
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
});
}
}
Let's say I have a form to create a store. I want to input its name, and the days the store will be open.
So I'll have a form, with some inputs, and I want to group checkboxes in one 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
});
}
It does not work and I can't figure why...
edit : thanks to @g-tranter (and others SO posts), I could fix the issue. The final code looks like :
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
});
}
}
Share
Improve this question
edited Jun 8, 2018 at 8:47
Bernard Pagoaga
asked Jun 6, 2018 at 21:56
Bernard PagoagaBernard Pagoaga
1,3961 gold badge11 silver badges17 bronze badges
2 Answers
Reset to default 9Maybe you need a mat-selection-list
Check Angular Material documentation
Hope it helps you!
You are setting the form control name to a numeric index:
formControlName="{{i}}"
which doesn't exist in the form group.
You need to set it to "monday" etc. or set
[formControl]="availableDay"
本文标签: javascriptHow to group several checkboxes with angularmaterialStack Overflow
版权声明:本文标题:javascript - How to group several checkboxes with angular-material - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739359437a2159747.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论