Skip to content

Add support for clearing FormArray #18531

Closed
@wanton7

Description

@wanton7

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[X] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Current way of clearing FormArray

while (rows.lenght) {
  rows.removeAt(0);
}

Expected behavior

rows.length = 0;

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Angular version: 4.3


Browser:
- [X] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Activity

added this to the Backlog milestone on Jan 23, 2018
sinedsem

sinedsem commented on Feb 26, 2018

@sinedsem

+1

renanmontebelo

renanmontebelo commented on Feb 28, 2019

@renanmontebelo
Contributor

Check this way:

rows.controls.splice(0)

As per https://angular.io/api/forms/FormArray#adding-or-removing-controls-from-a-form-array this is not recommended.

KostyaTretyak

KostyaTretyak commented on Mar 2, 2019

@KostyaTretyak
Contributor

As per Adding or removing controls from a form array this is not recommended.

@renanmontebelo, yes, you are right. Now I am not recommended this way.

If we look at the source of removeAt() method, we can see that for properly way needed to use not only controls.splice().

renanmontebelo

renanmontebelo commented on Mar 8, 2019

@renanmontebelo
Contributor

As of Angular 8+ please use:

formArray.clear();

For previous versions the recommended way is

// updated based on trotyl comment
while (formArray.length) {
  formArray.removeAt(formArray.length-1);
}
trotyl

trotyl commented on Mar 9, 2019

@trotyl
Contributor

@renanmontebelo Arrays are in consecutive memory, removing first element costs O(n), and the while loop becomes O(n^2), better to remove at end.

renanmontebelo

renanmontebelo commented on Mar 9, 2019

@renanmontebelo
Contributor

@trotyl you're absolutely right, thank you for your suggestion. I updated my comment based on your feedback.

added a commit that references this issue on Apr 17, 2019
7c055c9
rammaradolla

rammaradolla commented on May 16, 2019

@rammaradolla

While loop will take long time to delete all items if array has 100's of items. You can empty both controls and value properties of FormArray like below.

clearFormArray = (formArray: FormArray) => {
formArray.controls = [];
formArray.setValue([]);
}

angular-automatic-lock-bot

angular-automatic-lock-bot commented on Sep 15, 2019

@angular-automatic-lock-bot

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.

locked and limited conversation to collaborators on Sep 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @vicb@wanton7@KostyaTretyak@kara@trotyl

      Issue actions

        Add support for clearing FormArray · Issue #18531 · angular/angular