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 · 8 comments
Closed

Add support for clearing FormArray #18531

wanton7 opened this issue Aug 4, 2017 · 8 comments
Labels
area: forms feature Issue that requests a new feature freq2: medium
Milestone

Comments

@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
Copy link

+1

@renanmontebelo
Copy link
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
Copy link
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().

@kara kara closed this as completed in a68b1a1 Mar 8, 2019
@renanmontebelo
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
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
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.

wKoza pushed a commit to wKoza/angular that referenced this issue Apr 17, 2019
…28918)

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

PR Close angular#28918
@rammaradolla
Copy link

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
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: forms feature Issue that requests a new feature freq2: medium
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants