Angular + Material-如何使用FormGroup处理多个复选框 [英] Angular + Material - How to handle multiple checkboxes with formgroup
问题描述
我有一个我从后端得到的兴趣清单.我希望用户能够选择他们想要的兴趣.我将只将他们检查的兴趣存储在数据库中,并在他们加载页面时进行预填充.但是首先我需要获得用户选择的这些兴趣.
I have a list of interests i am getting from the backend. I want a user to be able to select the interests they want. I will store only the interests they checked in the DB and pre-populate when they load the page. But first i need to get these interests the user has selected.
interest.component.ts
interest.component.ts
export class InterestsComponent implements OnInit {
interestFormGroup : FormGroup
interests:any;
selected: any;
constructor(public interestService: InterestService, private formBuilder: FormBuilder,
) { }
ngOnInit() {
this.interestFormGroup = this.formBuilder.group({
interests: this.formBuilder.array([])
});
this.interestService.all().subscribe((res) => {
this.interests = res;
});
}
change() {
console.log(this.interestFormGroup.value);
}
}
interest.component.html
interest.component.html
<div id="interest">
<div class="interest-list">
<form [formGroup]="interestFormGroup">
<div *ngFor="let interest of interests" >
<mat-checkbox class="example-margin" formNameArray="interests" (change)="change()">{{interest}}</mat-checkbox>
</div>
</form>
</div>
</div>
在我的console.log上的change事件上,它显示没有将任何值添加到interestFormGroup内的interest数组中.甚至选中该复选框.
In my console.log on the change event it shows there is no values being added to the interests array within the interestFormGroup. Even tho the checkboxes are checked.
推荐答案
您应手动将控件添加到FormArray
中,例如:
You should add controls to FormArray
manually like:
this.interests = res;
const formArray = this.interestFormGroup.get('interests') as FormArray;
this.interests.forEach(x => formArray.push(new FormControl(false)));
然后更改模板,如下所示:
then change template as follows:
<form [formGroup]="interestFormGroup" (ngSubmit)="submit()">
<ng-container formArrayName="interests">
<div *ngFor="let interest of interests; let i = index" >
<mat-checkbox class="example-margin" [formControlName]="i">
{{interest}}
</mat-checkbox>
</div>
</ng-container>
<button>Submit</button>
</form>
提交表单时,您需要将FormArray
值转换为所需的结果:
And when you will submit form you need to transform FormArray
values to your desired result:
submit() {
const result = Object.assign({},
this.interestFormGroup.value, {
interests: this.interests
.filter((x, i) => !!this.interestFormGroup.value.interests[i])});
console.log(result);
}
另一种解决方案是监听change
事件,然后手动将值添加和删除到FormArray
:
Alternative solution is listen change
event and manually add and remove value to FormArray
:
<div *ngFor="let interest of interests; let i = index" >
<mat-checkbox class="example-margin"
(change)="onChange($event)" [value]="interest">{{interest}}</mat-checkbox>
</div>
onChange(event) {
const interests = <FormArray>this.interestFormGroup.get('interests') as FormArray;
if(event.checked) {
interests.push(new FormControl(event.source.value))
} else {
const i = interests.controls.findIndex(x => x.value === event.source.value);
interests.removeAt(i);
}
}
Stackblitz example
这篇关于Angular + Material-如何使用FormGroup处理多个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!