Provide a way to identify item of FormArray other than index
See original GitHub issueI’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
Index of array seems to be only way to identify item of FormControl
in FormArray
.
If a form has sort functionality, there is no way to identify sorted items.
ngOnInit() {
this.service.fetchTodos.subscribe(todos => {
// todos: [ { id: 1, content: 'foo', sortNo: 0 }, { id: 2, content: 'bar', sortNo: 1 } ]
this.form = this.fb.group({
// user can edit content and sortNo by drag&drop
todos: this.fb.array(todos.map(t => t.content)),
});
});
}
onSubmit() {
const value = this.form.value;
// { todos: [ 'foo yay', 'barrr' ] }
// if todos are sortable, { id: 1, content: 'foo yay', sortNo: 0 } or { id: 2, content: 'foo yay', sortNo: 0 } is indeterminable
...
}
Expected behavior
FormControl
or AbstractControl
has a way to identify themselves, ex: FormControl#id
like
ngOnInit() {
this.service.fetchTodos.subscribe(todos => {
// todos: [ { id: 1, content: 'foo', sortNo: 0 }, { id: 2, content: 'bar', sortNo: 1 } ]
this.form = this.fb.group({
todos: this.fb.array(todos.map(t => { value: t.content, id: t.id })),
});
});
}
onSubmit() {
const value = this.form.value;
// { todos: [ 'foo yay', 'barrr' ] }
const controls = this.form.get('todos').controls.map((c, i) => { id: c.id, value: c.value, sortNo: i });
// [ { id: 2, value: 'foo yay', sortNo: 0 }, { id: 1, value: 'barrr', sortNo: 1 } ]
}
Minimal reproduction of the problem with instructions
https://stackblitz.com/edit/angular-zcvvg4
What is the motivation / use case for changing the behavior?
I want to sort items of FormArray
Environment
Angular version: 4.3.6
Browser:
- [x] Chrome (desktop) version 61
- [ ] 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: 8.2.1
- Platform: Mac
Others:
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (3 by maintainers)
Top Results From Across the Web
angular - How to identify which item in FormArray emitted ...
When binding controls of FormArray to an array in your model, you can pass index or any other id from the model to...
Read more >FormArray - Angular
Tracks the value and validity state of an array of FormControl , FormGroup or FormArray instances.
Read more >Working with Angular FormArray - JScrambler Blog
FormArray provides a way to collect the dynamically created forms in one place. You can access each of the forms using the index...
Read more >Angular Reactive Forms: The Ultimate Guide to FormArray
In this article, I'd like to discuss this peculiar creation — a FormArray, exposed by Angular Reactive Forms. We'll learn when and how...
Read more >Indexed collections - JavaScript - MDN Web Docs - Mozilla
The Array object has methods for manipulating arrays in various ways, such as joining, ... It's shorter than other forms of array creation, ......
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 Free
Top 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
Never mind, I found the solution too. I used “findIndex” method of the abstract control:
index = (<FormArray>this.myForm.get('myFormArray')).controls.findIndex(x => x.value.id === myConditionId);
I handled this by another way, so closing this issue.