Title: Changing form control name attribute issue. · Issue #38465 · angular/angular · GitHub
Open Graph Title: Changing form control name attribute issue. · Issue #38465 · angular/angular
X Title: Changing form control name attribute issue. · Issue #38465 · angular/angular
Description: 🐞 bug report Affected Package @angular/forms Is this a regression? The issue is not a regression. It is applicable to all Angular versions. Description The problem arises in the template driven form (with NgModel) when you change the nam...
Open Graph Description: 🐞 bug report Affected Package @angular/forms Is this a regression? The issue is not a regression. It is applicable to all Angular versions. Description The problem arises in the template driven for...
X Description: 🐞 bug report Affected Package @angular/forms Is this a regression? The issue is not a regression. It is applicable to all Angular versions. Description The problem arises in the template driven for...
Opengraph URL: https://github.com/angular/angular/issues/38465
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Changing form control name attribute issue.","articleBody":"# 🐞 bug report\r\n\r\n### Affected Package\r\n@angular/forms\r\n\r\n### Is this a regression?\r\nThe issue is not a regression. It is applicable to all Angular versions.\r\n\r\n### Description\r\n\r\nThe problem arises in the template driven form (with NgModel) when you change the name attribute of the control in the DOM. If one has, say, 4 `\u003cinput>` fields in an ngFor loop with their own values and name attributes that correspond to the array’s indexes, and removes whichever `\u003cinput>` field but the last one, and then adds a new `\u003cinput>` field, the default value of the newly-added field replaces the value of the third `\u003cinput>` field. That is, the DOM element’s value is affected by the value of the newly-added field, whereas the values of the array elements behind the DOM elements are not. \r\n\r\nThe problem has to do with the FormControls in the FormGroup, related to the rendered DOM elements, and the naming convention for FormControls of using indexes. Removing an `\u003cinput>` DOM element does not change the names of the rest underlying FormControls within the FormGroup even if the name attribute of the control has been changed. It just reorganises the indexes of the DOM elements after the removed field. Thus, when a new `\u003cinput>` element is added after the removal of an element, its FormControl’s name is created with index 3, which is already used in the name of the FormControl for the second last element, and the new FormControl eventually points to the two last `\u003cinput>` DOM elements at the same time.\r\n\r\nTo sum up, we want to lay down the following two considerations. First, what is the reason for the NgModel not to follow the changes in the names of DOM elements and not to update FormControls in the FormGroup? Second, we know that trackBy solves our problem, as suggested in a number of posts online such as [this one](https://github.com/angular/angular/issues/21068#issuecomment-352882911), but we believe that this is a workaround and the real issue is circumvented. That is why we would like to propose another solution with the following patch:\r\n```\r\n--- old_ng_model.js\t2020-08-13 11:12:38.000000000 +0300\r\n+++ ng_model.js\t2020-08-13 11:03:54.000000000 +0300\r\n@@ -151,6 +151,15 @@\r\n this._checkForErrors();\r\n if (!this._registered)\r\n this._setUpControl();\r\n+ if (this._registered && 'name' in changes && changes.name['previousValue']){\r\n+ resolvedPromise.then(() => {\r\n+ let currControl = Object.assign(new NgModel, this);\r\n+ currControl.name = changes.name['previousValue'];\r\n+ return this.formDirective.removeControl(currControl);\r\n+ }).then(() => {\r\n+ this._setUpControl();\r\n+ });\r\n+ }\r\n if ('isDisabled' in changes) {\r\n this._updateDisabled(changes);\r\n }\r\n```\r\n\r\n\r\n\r\n## 🔬 Minimal Reproduction\r\nHere’s the code for the minimal reproduction of the problem: https://stackblitz.com/edit/angular-ivy-p8fd6d?file=src%2Fapp%2Fapp.component.html\r\n\r\n##### Steps to Exemplify the Problem:\r\nadd four new elements. The page renders the four `\u003cinput>` fields, the indexes of the elements, and the array of elements behind the `\u003cinput>` fields;\r\nremove an element but the last one;\r\nadd a new element.\r\n\r\n\r\n##### Current behaviour\r\nThe default value of the newly-added element replaces the value of the second last element; the value of the second last element in the `app.component.ts`’s `record` array is not changed.\r\n\r\n##### Expected behaviour\r\n\r\nIt is expected that the addition of a new `\u003cinput>` element should not affect the values of any of the other DOM elements.\r\n\r\n\r\n## 🔥 Exception or Error\r\n\r\nNo exceptions or errors.\r\n\r\n\r\n## 🌍 Your Environment\r\n\r\nThe example is taken from **Angular Version 10**, but the issue is applicable to **all Angular Versions**.","author":{"url":"https://github.com/kirilkalchev","@type":"Person","name":"kirilkalchev"},"datePublished":"2020-08-14T10:42:58Z","url":"https://github.com/angular/angular/issues/38465"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:5ae84242-bc83-ba98-866c-f944327da9d9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C124:1148D3:1A5947B:21A83C3:6B0150EB |
| html-safe-nonce | c7d5d90967bcb5c548e779c18cf77a2c65482187481f450dbf3d2147ad5b2426 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTI0OjExNDhEMzoxQTU5NDdCOjIxQTgzQzM6NkIwMTUwRUIiLCJ2aXNpdG9yX2lkIjoiNDk5NjcwNjYzMjYwMDczNTk3OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 13a03e4d91174191544d73ce7fe87ff11e37cb1f06279da6289d1bedbe5411fc |
| hovercard-subject-tag | issue:679071461 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/38465/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4542fd25ac84633ee40725cdc9a254e097b42e042f3fb6637b713f6a2b0eb68d/angular/angular/issues/38465 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4542fd25ac84633ee40725cdc9a254e097b42e042f3fb6637b713f6a2b0eb68d/angular/angular/issues/38465 |
| og:image:alt | 🐞 bug report Affected Package @angular/forms Is this a regression? The issue is not a regression. It is applicable to all Angular versions. Description The problem arises in the template driven for... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kirilkalchev |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width