Title: [BUG]: Component implementing ControlValueAccessor used inside ngFor loses data · Issue #37920 · angular/angular · GitHub
Open Graph Title: [BUG]: Component implementing ControlValueAccessor used inside ngFor loses data · Issue #37920 · angular/angular
X Title: [BUG]: Component implementing ControlValueAccessor used inside ngFor loses data · Issue #37920 · angular/angular
Description: 🐞 bug report Affected Package The issue is caused by package @angular/forms. Angular 8+ (I haven't tested in versions before v8.0). Is this a regression? No, this bug happens since v8.0, and remains in v10. Description This bug occurs wh...
Open Graph Description: 🐞 bug report Affected Package The issue is caused by package @angular/forms. Angular 8+ (I haven't tested in versions before v8.0). Is this a regression? No, this bug happens since v8.0, and remain...
X Description: 🐞 bug report Affected Package The issue is caused by package @angular/forms. Angular 8+ (I haven't tested in versions before v8.0). Is this a regression? No, this bug happens since v8.0, and re...
Opengraph URL: https://github.com/angular/angular/issues/37920
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[BUG]: Component implementing ControlValueAccessor used inside ngFor loses data","articleBody":"# 🐞 bug report\r\n\r\n### Affected Package\r\nThe issue is caused by package @angular/forms.\r\nAngular 8+ (I haven't tested in versions before v8.0).\r\n\r\n### Is this a regression?\r\n\r\nNo, this bug happens since v8.0, and remains in v10.\r\n\r\n### Description\r\n\r\nThis bug occurs when you have a component that implements ControlValueAccessor and it's used inside an *ngFor.\r\nDependending on the order that you add and remove the elements, some data are lost.\r\n```typescript\r\nimport { Component, VERSION } from '@angular/core';\r\n\r\n@Component({\r\n selector: 'my-app',\r\n template: `\r\n \u003cform>\r\n \u003cdiv *ngFor=\"let data of dataList; index as i;\">\r\n \u003clabel for=\"inputDescription{{i}}\">Description\u003c/label>\r\n \u003csample-input\r\n [(ngModel)]=\"data.description\"\r\n [ngModelOptions]=\"{ standalone: true }\"\r\n [index]=\"i\">\u003c/sample-input>\r\n \u003cbutton type=\"button\" (click)=\"removeData(i)\">Remove\u003c/button>\r\n \u003c/div>\r\n \u003cdiv>\r\n \u003cbutton type=\"button\" (click)=\"addData()\">Add\u003c/button>\r\n \u003c/div>\r\n \u003cdiv>\r\n \u003cpre>{{ dataList | json }}\u003c/pre>\r\n \u003c/div>\r\n\u003c/form>\r\n `\r\n})\r\nexport class AppComponent {\r\n name = 'Angular ' + VERSION.major;\r\n\r\n dataList = [];\r\n\r\n addData() {\r\n this.dataList.push({ description: '' });\r\n }\r\n\r\n removeData(index) {\r\n this.dataList.splice(index, 1);\r\n }\r\n}\r\n```\r\n```typescript\r\nimport { Component, StaticProvider, forwardRef, Input } from '@angular/core';\r\nimport { ControlValueAccessor, NG_VALUE_ACCESSOR, NgForm, ControlContainer } from '@angular/forms';\r\n\r\nexport const SAMPLE_INPUT_VALUE_ACCESSOR: StaticProvider = {\r\n provide: NG_VALUE_ACCESSOR,\r\n useExisting: forwardRef(() => SampleInputComponent),\r\n multi: true\r\n};\r\n\r\n@Component({\r\n selector: 'sample-input',\r\n template: `\u003cinput type=\"text\" id=\"inputDescription{{ index }}\" [(ngModel)]=\"value\" name=\"inputDescription{{ index }}\" />`,\r\n providers: [SAMPLE_INPUT_VALUE_ACCESSOR],\r\n viewProviders: [ { provide: ControlContainer, useExisting: NgForm } ]\r\n})\r\nexport class SampleInputComponent implements ControlValueAccessor {\r\n\r\n @Input() index;\r\n\r\n private onChange: (_: any) => { };\r\n private onTouched: () => { };\r\n private _value: any;\r\n\r\n public get value() {\r\n return this._value;\r\n }\r\n\r\n public set value(value: any) {\r\n this._value = value;\r\n this.onChange(value);\r\n }\r\n\r\n writeValue(obj: any): void {\r\n this._value = obj;\r\n }\r\n\r\n registerOnChange(fn: any): void {\r\n this.onChange = fn;\r\n }\r\n\r\n registerOnTouched(fn: any): void {\r\n this.onTouched = fn;\r\n }\r\n\r\n setDisabledState(isDisabled: boolean): void {\r\n\r\n }\r\n}\r\n```\r\n\r\n## 🔬 Minimal Reproduction\r\n1 - Insert 4 elements (with value):\r\n\r\n2 - Then remove the first element:\r\n\r\n3 - Insert new element:\r\n\r\n**Problem: value 4 is gone**\r\n\r\nExample: [https://stackblitz.com/edit/angular-ivy-uqpssy](https://stackblitz.com/edit/angular-ivy-uqpssy)\r\n\r\n## 🔥 Exception or Error\r\nNone\r\n\r\n\r\n## 🌍 Your Environment\r\n\r\n**Angular Version:**\r\n\u003cpre>\u003ccode>@angular/animations10.0.1\r\n@angular/common10.0.1\r\n@angular/compiler10.0.1\r\n@angular/core10.0.1\r\n@angular/forms10.0.1\r\n@angular/platform-browser10.0.1\r\n@angular/platform-browser-dynamic10.0.1\r\n@angular/router10.0.1\r\nrxjs6.5.5\r\ntslib2.0.0\r\nzone.js\r\n\u003c/code>\u003c/pre>\r\n\r\n**Anything else relevant?**\r\nChrome version: 83.0.4103.116 - 64 bits","author":{"url":"https://github.com/rafaelmfonseca","@type":"Person","name":"rafaelmfonseca"},"datePublished":"2020-07-04T02:53:17Z","url":"https://github.com/angular/angular/issues/37920"}
| 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:3b93d316-480b-9680-9c0c-1a9f949d93a7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EA0E:317608:1BF6F76:236965E:6B015174 |
| html-safe-nonce | 09db141cb493a46e122981f2858c1fce189aa80adeb3bb8d3c1a4f0db52096b6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQTBFOjMxNzYwODoxQkY2Rjc2OjIzNjk2NUU6NkIwMTUxNzQiLCJ2aXNpdG9yX2lkIjoiMzU5NjE3NDMzMjAyOTY1MzM2NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 94b68e961a3344aa353babce9fbfbb3d0e1234adf1e093c5eb58fd07c5d10720 |
| hovercard-subject-tag | issue:650815502 |
| 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/37920/issue_layout |
| twitter:image | https://opengraph.githubassets.com/70d9046ff579f72c91c3c55c4bf48a18604aefc1084961bcf29a3492fb470f3c/angular/angular/issues/37920 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/70d9046ff579f72c91c3c55c4bf48a18604aefc1084961bcf29a3492fb470f3c/angular/angular/issues/37920 |
| og:image:alt | 🐞 bug report Affected Package The issue is caused by package @angular/forms. Angular 8+ (I haven't tested in versions before v8.0). Is this a regression? No, this bug happens since v8.0, and remain... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rafaelmfonseca |
| 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