Closed
Description
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
sinedsem commentedon Feb 26, 2018
+1
renanmontebelo commentedon Feb 28, 2019
As per https://angular.io/api/forms/FormArray#adding-or-removing-controls-from-a-form-array this is not recommended.
KostyaTretyak commentedon Mar 2, 2019
@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 commentedon Mar 8, 2019
As of Angular 8+ please use:
formArray.clear();
For previous versions the recommended way is
trotyl commentedon Mar 9, 2019
@renanmontebelo
Array
s are in consecutive memory, removing first element costsO(n)
, and thewhile
loop becomesO(n^2)
, better to remove at end.renanmontebelo commentedon Mar 9, 2019
@trotyl you're absolutely right, thank you for your suggestion. I updated my comment based on your feedback.
feat(forms): clear (remove all) components from a FormArray (angular#…
rammaradolla commentedon May 16, 2019
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 commentedon Sep 15, 2019
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.