如何处理 Angular 2 RC5 中的复选框组?
How to handle checkbox group in Angular 2 RC5?
我有一个表单,我希望用户可以在其中编辑他希望接收的杂志订阅。代码如下:
组件:
export class OrderFormComponent {
subscriptions = [
{id: 'weekly', display: 'Weekly newsletter'},
{id: 'monthly', display: 'Monthly newsletter'},
{id: 'quarterly', display: 'Quarterly newsletter'},
];
mySubs = [
this.subscriptions[1]
]
order = new FormGroup({
subs: new FormArray(this.mySubs.map(sub => new FormControl(sub)), Validations.required) //Lost at this part
});
}
模板:
<form [formGroup]="order">
<div formArrayName="subs">
<label>Sign me up for newsletters</label>
<p *ngFor="let s of subscriptions; let i=index">
<input type="checkbox" [value]="s.id" [formControlName]="i" /> {{ s.display }}
</p>
</div>
<div>
<input type="checkbox" formControlName="agree" /> I agree to the terms and conditions.
</div>
{{ order.value | json }}
当我 运行 应用程序时,显示了三个复选框,但只有一个被选中(那个是错误的)。选中的那个有标签,而其他的没有。
我做错了什么?
您在实例化 FormControl
时犯了一个错误。您没有为所有选项创建一个 FormControl
的新实例,而是只创建了一个(通过遍历 mySub
)。将您的代码更改为如下内容(未测试):
order = new FormGroup({
// creating an array of form control with default values
subs: new FormArray(this.subscriptions.map(sub => new FormControl(this.isSelectedSub(sub))), Validations.required)
});
//...
isSelectedSub(sub): boolean {
return this.mySubs.indexOf(sub) >= 0;
}
并且您可能需要一个功能来在发送之前将复选框合并到一组选定的杂志订阅中。
好吧,我终于明白了。
在我的组件中我有:
// The order retrieved from the server
subscription = {
schedules: [{id: 'weekly', display: 'Weekly update'}],
}
//The FormGroup element
this.subscriptionForm = new FormGroup({
//Here I fill up a FormArray with some FormControls initialized to the
//currently selected schedules
schedules: new FormArray(this.subscription.schedules.map(schedule => new FormControl(schedule)), Validators.minLength(1))
});
在我看来:
<div>
<label>Frequency</label>
<p *ngFor="let schedule of viewData.schedules">
<input type="checkbox"
[checked]="subscription.schedules.includes(schedule)"
(change)="changeSchedules(schedule)"> {{ schedule.display }}
</p>
</div>
这里是 class 中的 changeSchedules()
方法:
changeSchedules(schedule: any) {
var currentScheduleControls: FormArray = this.subscriptionForm.get('schedules') as FormArray;
var index = currentScheduleControls.value.indexOf(schedule);
if(index > -1) currentScheduleControls.removeAt(index) //If the user currently uses this schedule, remove it.
else currentScheduleControls.push(new FormControl(schedule)); //Otherwise add this schedule.
}
很有魅力!表单按预期进行验证,在提交表单之前不需要额外的方法 retrieve/consolidate 订阅数组。
我有一个表单,我希望用户可以在其中编辑他希望接收的杂志订阅。代码如下:
组件:
export class OrderFormComponent {
subscriptions = [
{id: 'weekly', display: 'Weekly newsletter'},
{id: 'monthly', display: 'Monthly newsletter'},
{id: 'quarterly', display: 'Quarterly newsletter'},
];
mySubs = [
this.subscriptions[1]
]
order = new FormGroup({
subs: new FormArray(this.mySubs.map(sub => new FormControl(sub)), Validations.required) //Lost at this part
});
}
模板:
<form [formGroup]="order">
<div formArrayName="subs">
<label>Sign me up for newsletters</label>
<p *ngFor="let s of subscriptions; let i=index">
<input type="checkbox" [value]="s.id" [formControlName]="i" /> {{ s.display }}
</p>
</div>
<div>
<input type="checkbox" formControlName="agree" /> I agree to the terms and conditions.
</div>
{{ order.value | json }}
当我 运行 应用程序时,显示了三个复选框,但只有一个被选中(那个是错误的)。选中的那个有标签,而其他的没有。
我做错了什么?
您在实例化 FormControl
时犯了一个错误。您没有为所有选项创建一个 FormControl
的新实例,而是只创建了一个(通过遍历 mySub
)。将您的代码更改为如下内容(未测试):
order = new FormGroup({
// creating an array of form control with default values
subs: new FormArray(this.subscriptions.map(sub => new FormControl(this.isSelectedSub(sub))), Validations.required)
});
//...
isSelectedSub(sub): boolean {
return this.mySubs.indexOf(sub) >= 0;
}
并且您可能需要一个功能来在发送之前将复选框合并到一组选定的杂志订阅中。
好吧,我终于明白了。
在我的组件中我有:
// The order retrieved from the server
subscription = {
schedules: [{id: 'weekly', display: 'Weekly update'}],
}
//The FormGroup element
this.subscriptionForm = new FormGroup({
//Here I fill up a FormArray with some FormControls initialized to the
//currently selected schedules
schedules: new FormArray(this.subscription.schedules.map(schedule => new FormControl(schedule)), Validators.minLength(1))
});
在我看来:
<div>
<label>Frequency</label>
<p *ngFor="let schedule of viewData.schedules">
<input type="checkbox"
[checked]="subscription.schedules.includes(schedule)"
(change)="changeSchedules(schedule)"> {{ schedule.display }}
</p>
</div>
这里是 class 中的 changeSchedules()
方法:
changeSchedules(schedule: any) {
var currentScheduleControls: FormArray = this.subscriptionForm.get('schedules') as FormArray;
var index = currentScheduleControls.value.indexOf(schedule);
if(index > -1) currentScheduleControls.removeAt(index) //If the user currently uses this schedule, remove it.
else currentScheduleControls.push(new FormControl(schedule)); //Otherwise add this schedule.
}
很有魅力!表单按预期进行验证,在提交表单之前不需要额外的方法 retrieve/consolidate 订阅数组。