Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for clearing FormArray #18531

Closed
wanton7 opened this Issue Aug 4, 2017 · 6 comments

Comments

Projects
None yet
7 participants
@wanton7
Copy link

wanton7 commented Aug 4, 2017

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:

@sinedsem

This comment has been minimized.

Copy link

sinedsem commented Feb 26, 2018

+1

renanmontebelo added a commit to renanmontebelo/angular that referenced this issue Feb 22, 2019

renanmontebelo added a commit to renanmontebelo/angular that referenced this issue Feb 23, 2019

renanmontebelo added a commit to renanmontebelo/angular that referenced this issue Feb 23, 2019

renanmontebelo added a commit to renanmontebelo/angular that referenced this issue Feb 28, 2019

feat(forms): clear (remove all) components from a FormArray
This method is a more convenient and efficient way of removing all
components from a FormArray. Before it, we needed to loop the FormArray
removing each component until empty.

Resolves angular#18531
@renanmontebelo

This comment has been minimized.

Copy link
Contributor

renanmontebelo commented Feb 28, 2019

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

This comment has been minimized.

Copy link

KostyaTretyak commented Mar 2, 2019

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().

@kara kara closed this in a68b1a1 Mar 8, 2019

@renanmontebelo

This comment has been minimized.

Copy link
Contributor

renanmontebelo commented Mar 8, 2019

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

This comment has been minimized.

Copy link
Contributor

trotyl commented Mar 9, 2019

@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

This comment has been minimized.

Copy link
Contributor

renanmontebelo commented Mar 9, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.