forms: Support negative indexes in `FormArray`
See original GitHub issueWhich @angular/* package(s) are relevant/releated to the feature request?
forms
Description
Nowadays attempting to retrieve a control in a negative index returns undefined
(due to underlying usage of native Array
).
I would like FormArray
to support relative indexing. Methods like at()
and removeAt()
could support negative integers as indexes, which count back from the last item in the array.
Proposed solution
Similar to the ES2021 Array.prototype.at()
, support negative integers as indexes:
// the following are equivalent:
formArray.at(-1)
formArray.at(formArray.length - 1)
Methods which should support relative indexing:
at()
removeAt()
insert()
setControl()
See proposed solution implementation in https://github.com/angular/angular/pull/44631
Alternatives considered
One could use formArray.at(formArray.length - 1)
like in indices of native Array
s pre-ES2021, but this can be exhausting and causes longer lines of code.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:40
- Comments:8 (2 by maintainers)
Top Results From Across the Web
FormArray - Angular
Index in the array to retrieve the control. If index is negative, it will wrap around from the back, and if index is...
Read more >Angular Reactive Forms: Learn How To Use the FormArray
In this video, we will see how to create an #Angular Reactive Form using a form array, and furthermore, we will see how...
Read more >Angular on Twitter: "Support negative integers as indices in ...
Support negative integers as indices in FormArray! ES2021 was recently updated with a new http://Array.at method, which supports negative ...
Read more >Angular Reactive Forms - FormArray start from another index
I have an invoice template and want to start from another index on the second page, if the service fields doesnt fit on...
Read more >Angular: Build reactive forms using Formbuilder
Angular supports two designs for dynamic control. ... Import FormBuilder , Validators , and FormArray from @angular/forms . FormBuilder is required to ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@amitbeck thanks for creating this feature request. According to the Feature Request process (documented here), this ticket reached 20 upvotes and we’ve discussed it with the Angular team.
Given the fact that:
FormArray
class represents an Array data structure and using negative indices is common in array methods (such assplice
andat
)at
function)We’d like to proceed with this feature request. I’ll reopen PR #44631 and provide some feedback there.
Thank you.
Very Important!