Title: Directive that extends from DefaultValueAccessor applies to all controls instead of respecting the selector. Host's events not being trigerred · Issue #9146 · angular/angular · GitHub
Open Graph Title: Directive that extends from DefaultValueAccessor applies to all controls instead of respecting the selector. Host's events not being trigerred · Issue #9146 · angular/angular
X Title: Directive that extends from DefaultValueAccessor applies to all controls instead of respecting the selector. Host's events not being trigerred · Issue #9146 · angular/angular
Description: I'm submitting a ... bug report feature request support request => Please do not submit support request here, see note at the top of this template. Current behavior I am implementing masked input for text boxes. With ng1 I used to implem...
Open Graph Description: I'm submitting a ... bug report feature request support request => Please do not submit support request here, see note at the top of this template. Current behavior I am implementing masked input f...
X Description: I'm submitting a ... bug report feature request support request => Please do not submit support request here, see note at the top of this template. Current behavior I am implementing masked ...
Opengraph URL: https://github.com/angular/angular/issues/9146
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Directive that extends from DefaultValueAccessor applies to all controls instead of respecting the selector. Host's events not being trigerred","articleBody":"- **I'm submitting a ...**\n- [X] bug report\n- [ ] feature request\n- [ ] support request => Please do not submit support request here, see note at the top of this template.\n\n**Current behavior**\n\nI am implementing masked input for text boxes. With ng1 I used to implement using ngModelController's `$parsers` and `$formatters`. An example implementation can be found at https://plnkr.co/HQSyZkr4VgezzcSNWnuJ (ng1) . On this example the input displays uppercase text and stores (via ngModel) lowercase text.\n\nI managed to replicate the same behavior using `DefaultValueAccessor` as in in the following snippet (based on code posted on example posted on other issue). This code **works** on beta 17 but **doesn't work** on RC1. Obviously the beta 17 version differs on the import statements.\n\nThe code samples for beta 17 and RC 1 may be found here:\n\nhttps://plnkr.co/yDvm2x3YiKY6LKhRSzQ3 (Beta 17 - It works)\nhttp://plnkr.co/eAEML8VTnvNo78IFGgSB (RC1 - It doesn't work)\n\nWith RC1 the directive applies to all input controls in the form instead of respecting the selector `input[type=uppercase]`. Futhermore the `input` event is never triggered and nor the method `writeValue`\n\n``` typescript\nimport {Directive, Provider, forwardRef, Renderer, ElementRef} from '@angular/core';\nimport {DefaultValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS, Validator, Control} from '@angular/common';\n\nconst FORMAT_CPF_VALUE_ACCESSOR = new Provider(NG_VALUE_ACCESSOR, {\n useExisting: forwardRef(() => UpperCaseValueAccessor),\n multi: true\n});\n\n@Directive({\n selector: 'input[type=uppercase]',\n host: {\n \"maxlength\": \"14\",\n \"minlength\": \"14\",\n \"min\": \"0\",\n \"type\": \"text\",\n \"(input)\": \"input($event.target.value)\",\n \"(blur)\": \"blur()\"\n },\n bindings: [FORMAT_CPF_VALUE_ACCESSOR]\n})\nexport class UpperCaseValueAccessor extends DefaultValueAccessor {\n\n constructor(renderer: Renderer, elementRef: ElementRef) {\n elementRef.nativeElement.style.backgroundColor = 'yellow';\n super(renderer, elementRef);\n }\n\n ngAfterViewInit() { }\n\n blur() {\n this.onTouched();\n }\n\n input(value : string) {\n // Write back to model\n if (value) {\n value = value.toLowerCase();\n //write formatted to to control view\n (\u003cany>this)._elementRef.nativeElement.value = value.toUpperCase();\n }\n this.onChange(value);\n }\n\n writeValue(value: any): void {\n // Write to view\n if (value) {\n value = value.toUpperCase();\n }\n super.writeValue(value);\n }\n}\n```\n\nThe code for component using the directive : \n\n``` typescript\nimport {Component} from '@angular/core';\nimport {ControlGroup, Control, FormBuilder, Validators} from '@angular/common';\nimport {UpperCaseValueAccessor} from './uppercase-directive';\n\ninterface FormData {\n name : string;\n uppercaseText: string;\n}\n\n@Component({\n selector: 'my-app',\n providers: [],\n template: `\n \u003cform [ngFormModel]=\"dataForm\">\n \u003clabel for=\"inputName\">Name\u003c/label>\n \u003cinput type=\"text\" [(ngModel)]=\"data.name\" [ngFormControl]=\"dataForm.controls.name\" id=\"inputName\" placeholder=\"Name\">\n \u003cdiv [hidden]=\"dataForm.controls.name.valid || dataForm.controls.name.pristine\" class=\"alert alert-danger\">Name is required\u003c/div>\n\n \u003cp>\u003c/p>\n\n \u003clabel for=\"upperText\">Uppercase text\u003c/label>\n \u003cinput type=\"uppercase\" [(ngModel)]=\"data.uppercaseText\" [ngFormControl]=\"dataForm.controls.uppercaseText\" id=\"upperText\" placeholder=\"Something\">\n\n \u003cp>\u003c/p>\n\n \u003cbutton [disabled]=\"!dataForm.valid\" (click)=\"submitForm()\">Submit\u003c/button>\n \u003cbutton (click)=\"resetForm()\">Reset\u003c/button>\n\n \u003cp>\u003c/p>\n\n \u003cpre>{{data | json}}\u003c/pre>\n \u003c/form>\n `,\n directives : [UpperCaseValueAccessor],\n})\nexport class App {\n dataForm : ControlGroup; \n data : FormData = \u003cFormData> {};\n\n constructor(private fb : FormBuilder) {\n this.buildForm();\n }\n\n buildForm() {\n this.dataForm = this.fb.group({\n name : ['', Validators.required],\n uppercaseText : ['']\n });\n }\n\n submitForm() {\n console.log(this.dataForm.value);\n }\n\n resetForm() {\n this.buildForm();\n }\n}\n```\n\n**Expected/desired behavior**\n\nhttps://plnkr.co/yDvm2x3YiKY6LKhRSzQ3 (Beta 17 - It works)\nhttp://plnkr.co/eAEML8VTnvNo78IFGgSB (RC1 - It doesn't work)\n- **What is the expected behavior?**\n\nThe directive would apply only to componenet specified in the selector `input[type=uppercase]`, the event `input` would be triggered and the method `writeValue` would be invoked.\n- **Please tell us about your environment:**\n- **Angular version:** 2.0.0-RC.1 \n- **Browser:** [Chrome 51] \n- **Language:** [all | TypeScript 1.8.10] \n","author":{"url":"https://github.com/daniel-a-melo","@type":"Person","name":"daniel-a-melo"},"datePublished":"2016-06-10T21:44:01Z","url":"https://github.com/angular/angular/issues/9146"}
| 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:a39df659-09ef-085b-2886-9277ddfef4cc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C264:2AAD2E:A6CD0F:E02612:6B00EFFE |
| html-safe-nonce | f65305a2191116c52d09be87a46a47568b2a6dc5e4bfc386c7c8bcf20c6bf36e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMjY0OjJBQUQyRTpBNkNEMEY6RTAyNjEyOjZCMDBFRkZFIiwidmlzaXRvcl9pZCI6IjI5Mzk0MzQ0NTYyMTEwNTg2ODYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ac52fb33303b54121328be536e69166aa4e613fa11b24639fc7dc35001399220 |
| hovercard-subject-tag | issue:159729468 |
| 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/9146/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8e8b4fcd6485b27d788cb0f5a59287bb3b506c64d7afc84fcc689ddd3c29a8fb/angular/angular/issues/9146 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8e8b4fcd6485b27d788cb0f5a59287bb3b506c64d7afc84fcc689ddd3c29a8fb/angular/angular/issues/9146 |
| og:image:alt | I'm submitting a ... bug report feature request support request => Please do not submit support request here, see note at the top of this template. Current behavior I am implementing masked input f... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | daniel-a-melo |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width