question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

forms: Support negative indexes in `FormArray`

See original GitHub issue

Which @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:

  1. at()
  2. removeAt()
  3. insert()
  4. 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 Arrays pre-ES2021, but this can be exhausting and causes longer lines of code.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:40
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
AndrewKushnircommented, Jan 7, 2022

@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 as splice and at)
  • This feature would have minimal impact on the bundle size (we would even be able to pass the index as is in case of at function)

We’d like to proceed with this feature request. I’ll reopen PR #44631 and provide some feedback there.

Thank you.

2reactions
oran-intsightscommented, Jan 6, 2022

Very Important!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found