-
Notifications
You must be signed in to change notification settings - Fork 27k
Description
Which @angular/* package(s) are relevant/related to the feature request?
No response
Description
FormArray has its convenience method FormArray.clear to help with swiftly removing all controls. FormRecord and FormGroup should have theirs too.
Use case
When building a set of checkboxes, besides using FormArray for this, one can also use a FormGroup|FormRecord approach, where the set has a value in shape of an object:
this.fb.record({
[ key: string ]: this.fb.control(false | true)
});so that the value becomes very natural to read and understand and to set:
hasPrivateBathroom: true,
hasTv: false,
hasBalcony: true,
hasMinibar: true,
When building the form record out of an array, you'd have code along these lines somewhere:
buildOptionsRecord(availableOptions: Option[]): void {
// before constructing the FormRecord you need to first empty it
// instead of iterating over the members and having to `.removeControl()` by name
// I'd like to be able to say this.availableOptionsRecord.clear()
// and have it emptied in one go
// now build the controls
availableOptions.forEach(option => {
this.availableOptionsRecord.addControl(option.id, false)
});
}In response to the incoming array of available options you would need to first empty the current FormGroup|FormRecord. Currently this requires a very clumsy while loop.
Proposed solution
add FormGroup#clear() as well FormRecord#clear()
Alternatives considered
while(Object.keys(formGroup.controls).length){
const toRemove = Object.keys(formGroup.controls)[0];
formGroup.removeControl(toRemove)
}
No one want to have to write this type of code.