Title: Proposal to separate concerns of child control construction and parent ControlContainer mapping. · Issue #10195 · angular/angular · GitHub
Open Graph Title: Proposal to separate concerns of child control construction and parent ControlContainer mapping. · Issue #10195 · angular/angular
X Title: Proposal to separate concerns of child control construction and parent ControlContainer mapping. · Issue #10195 · angular/angular
Description: I'm submitting a ... (check one with "x") [ ] bug report [X] feature request [ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question Current be...
Open Graph Description: I'm submitting a ... (check one with "x") [ ] bug report [X] feature request [ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/ma...
X Description: I'm submitting a ... (check one with "x") [ ] bug report [X] feature request [ ] support request => Please do not submit support request here, instead see https://github.com/angula...
Opengraph URL: https://github.com/angular/angular/issues/10195
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal to separate concerns of child control construction and parent ControlContainer mapping.","articleBody":"**I'm submitting a ...** (check one with \"x\")\n\n```\n[ ] bug report\n[X] feature request\n[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question\n```\n\n**Current behavior**\n_Reactive forms:_\nThere is no built-in mechanism for separating the concerns of constructing a child control vs. mapping that hierarchy to a parent ControlContainer. I have two choices to handle this dynamic form mapping myself:\n- In the parent I can wire myself up to the proper lifecycle events to dynamically bind that child control to the correct parent member. I have to select the ViewChild, then implement OnChanges, tracking an initial wire-up flag.\n- In the child I can inject in the parent ControlContainer and receive the name/index of the parent.\n\n**Expected/desired behavior**\nA directive (or extension to existing __Name directives) which maps the value from the child control could remove this complexity and allow the child to specify the control while the directive (used in the parent component) would handle the mapping.\n\nIn other words, in the template I want to say this:\n\n``` html\n\u003caddress #homeAddress formControlName=\"homeAddress\" [childControl]=\"homeAddress.control\">\u003c/address>\n\u003caddress #workAddress formControlName=\"workAddress\" [childControl]=\"workAddress.control\">\u003c/address>\n```\n\nHere the value associated with childControl would be set on the parent ControlContainer (either array or group) based on the formControlName.\n\n**What is the motivation / use case for changing the behavior?**\nTake a reusable address component for an example that may build up a FormGroup of line1, line2, city, state, and zip, as well as several validators. I may want to use this component in several locations, perhaps on a page with a parent FormGroup with 'home' and 'work' children, and then on another page that allows me to add any number of saved shipping addresses to a FormArray.\n\n**Implementation**\nThis could be accomplished by adding an Input on the applicable directives (FormGroupName, FormControlName, NgControl). If set, then the control passed to it would be mapped to the parent ControlContainer. Here is a working hack I was toying with just for the sake of showing the interaction points and allowing me to use the above template syntax. The hack points are that it replaces those directives' ngOnChanges methods and accesses the private _parent.\n\n``` ts\n@Directive({\n selector: '[abcFormControl]'\n})\nexport class AbcFormControl implements OnChanges /*implements ControlValueAccessor*/ {\n constructor( @Optional() @Self() formGroupName: FormGroupName, @Optional() @Self() formArrayName: FormArrayName, @Optional() @Self() ngControl: NgControl) {\n this._otherDirective = \u003cany>(formGroupName ? formGroupName : formArrayName ? formArrayName : ngControl);\n this._oldOnChanges = this._otherDirective.ngOnChanges;\n this._otherDirective.ngOnChanges = (changes: SimpleChanges) => this._newOnChanges(changes);\n }\n\n @Input() abcFormControl: AbstractControl;\n private _otherDirective: any;\n private _oldOnChanges: (changes: SimpleChanges) => any;\n private _delayedChanges: SimpleChanges | null;\n\n private _newOnChanges(changes: SimpleChanges) {\n if (!this.abcFormControl)\n this._delayedChanges = changes;\n }\n\n ngOnChanges(changes: SimpleChanges) {\n if (changes[\"abcFormControl\"] && this._otherDirective._parent) {\n if (changes[\"abcFormControl\"].previousValue instanceof AbstractControl)\n this._remove();\n this._add();\n }\n if (this._delayedChanges) {\n this._oldOnChanges.call(this._otherDirective, this._delayedChanges);\n this._delayedChanges = null;\n }\n }\n\n private _add(): void {\n if (this._otherDirective._parent.control instanceof FormGroup)\n (\u003cFormGroup>this._otherDirective._parent.control).addControl(this._otherDirective.name, this.abcFormControl);\n else\n (\u003cFormArray>this._otherDirective._parent.control).insert(parseInt(this._otherDirective.name), this.abcFormControl);\n }\n\n private _remove(): void {\n if (this._otherDirective._parent.control instanceof FormGroup)\n (\u003cFormGroup>this._otherDirective._parent.control).removeControl(this._otherDirective.name);\n else\n (\u003cFormArray>this._otherDirective._parent.control).removeAt(parseInt(this._otherDirective.name));\n }\n}\n```\n\nThanks for considering!\n\n**Please tell us about your environment:**\n- **Angular version:** 2.0.0-rc.4\n- **Browser:** [Chrome 51] \n- **Language:** [TypeScript 2.0.0] \n","author":{"url":"https://github.com/EricABC","@type":"Person","name":"EricABC"},"datePublished":"2016-07-20T22:45:37Z","url":"https://github.com/angular/angular/issues/10195"}
| 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:fa6bb323-5586-0670-1bc0-8a1167d45877 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C8AA:2D96F1:1235D89:182230E:6B01187C |
| html-safe-nonce | cd1ab6f1b0edd6b12f58a3434855394161effb6b11b5eb6342f504750ef1283f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOEFBOjJEOTZGMToxMjM1RDg5OjE4MjIzMEU6NkIwMTE4N0MiLCJ2aXNpdG9yX2lkIjoiNDc5NjUxNjA5ODEwNTIxOTE5NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 19d4b5868c43793f7fcb8e9520d500544a9de977594c84e3e2844577e68d1d3b |
| hovercard-subject-tag | issue:166699976 |
| 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/10195/issue_layout |
| twitter:image | https://opengraph.githubassets.com/74e9c4683360b9dcd623cc118dfaad97c3ff2e1a8131c616b46527260ebb3282/angular/angular/issues/10195 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/74e9c4683360b9dcd623cc118dfaad97c3ff2e1a8131c616b46527260ebb3282/angular/angular/issues/10195 |
| og:image:alt | I'm submitting a ... (check one with "x") [ ] bug report [X] feature request [ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/ma... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | EricABC |
| 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