question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot call FormGroup removeControl method without emitting an event

See original GitHub issue

πŸš€ 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'];

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
etay2000commented, Mar 13, 2020

OK Ivy is out now, any chance we could get any action on this before everything is put on hold again to develop the next latest and greatest version?

0reactions
angular-automatic-lock-bot[bot]commented, Feb 20, 2021

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Select box not updating the UI after remove control of reactive ...
The problem is when I try to remove a feature column in between the first and the last feature column, the formControl is...
Read more >
FormGroup - Angular
This method does not update the value or validity of the control. ... Specifies whether this FormGroup instance should emit events after a...
Read more >
FormGroup in Angular - TekTutorialsHub
The FormGroup is one of the building blocks of the angular forms. ... When false, no events are emitted. ... removeControl(). This method...
Read more >
Angular FormGroup addControl() and removeControl()
The form control can be FormGroup , FormControl and FormArray . Find the methods of FormGroup used to add, update or remove controls....
Read more >
Reactive Forms Part 16 | Form Arrays removeAt method
In this video, you will learn how to remove a form controls from an array using removeAt method.To Know more about reactive forms,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found