Skip to content

Cannot call FormGroup removeControl method without emitting an event #29662

@MikeJerred

Description

@MikeJerred

🚀 feature request

Relevant Package

This feature request is for @angular/forms

Description

FormGroup has the methods addControl and registerControl, which allow you to add a control with and without emitting an event respectively. However, to remove a control there is only the removeControl method. Currently the cleanest way to remove a control without emitting an event is to delete it directly from the controls object and manually call _registerOnCollectionChange on the removed control.

Describe the solution you'd like

Two possible solutions:

  1. Add a method called unregisterControl that mirrors registerControl.
  2. Add an optional parameter to addControl and removeControl e.g. options: {emitEvent?: boolean} = {} and deprecate the registerControl method.

Describe alternatives you've considered

A workaround is to manually delete the control:

const form = new FormGroup({ foo: new FormControl() });

// to remove the control:
form.removeControl('foo');

// this is required in order to avoid emitting an event:
const controlToRemove = form.get('foo') as FormControl;
controlToRemove._registerOnCollectionChange(() => {});
delete form.controls['foo'];

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions