Title: Instantiate only one directive playing a particular role · Issue #3011 · angular/angular · GitHub
Open Graph Title: Instantiate only one directive playing a particular role · Issue #3011 · angular/angular
X Title: Instantiate only one directive playing a particular role · Issue #3011 · angular/angular
Description: We need to provide a way to tell the compiler not to instantiate a directive when there is another, more specialized, directive that does the same job. Imagine we have a default value accessor directive used by forms. @Directive({ select...
Open Graph Description: We need to provide a way to tell the compiler not to instantiate a directive when there is another, more specialized, directive that does the same job. Imagine we have a default value accessor dire...
X Description: We need to provide a way to tell the compiler not to instantiate a directive when there is another, more specialized, directive that does the same job. Imagine we have a default value accessor dire...
Opengraph URL: https://github.com/angular/angular/issues/3011
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Instantiate only one directive playing a particular role","articleBody":"We need to provide a way to tell the compiler not to instantiate a directive when there is another, more specialized, directive that does the same job.\n\nImagine we have a default value accessor directive used by forms. \n\n```\n@Directive({\n selector: '[ng-control]'\n})\nclass DefaultValueAccessor {}\n```\n\nNow, let's add a more specialized value accessor that can handle checkboxes.\n\n```\n@Directive({\n selector: \"input[type=checkbox][ng-control]\n})\nclass CheckeboxValueAccessor {}\n```\n\nIn the following template I would like the first input to use DefaultValueAccessor, and the second input to use CheckeboxValueAccessor. \n\n```\n\u003cinput type=\"text\" ng-control=\"name\">\n\u003cinput type=\"checkbox\" ng-control=\"isAmerican\">\n```\n\nUnfortunately, the second input will end up having two value accessors. So this approach does not work.\n\nWhat we have to do right now is to write mutually exclusive selectors that look like this:\n\n```\n@Directive({\n selector: 'input:not([type=checkbox])[ng-control],textarea[ng-control]'\n})\nclass DefaultValueAccessor {}\n```\n\nEven though this approach is not elegant, it sort of works when one person/team provides a set of directives. If, however, I would like to add a third-party value accessor, I will end up having the same problem: I will have two value accessors created for the same element.\n## Proposal 1\n\nHave a mechanism to tell the compiler that all value accessors play the same role. So the compiler can instantiate only one for each element. The compiler can take the last directives from the list of directives that matches a element.\n\n```\n@Directive({selector: '[ng-control]', role: 'valueAccessor')\nclass DefaultValueAccessor{}\n\n@Directive({selector: 'some custom stuff', role: 'valueAccessor')\nclass CustomValueAccessor{}\n\nconst directives = [DefaultValueAccessor, CustomValueAccessor];\n\nIf CustomValueAccessor matches the element, DefaultValueAccessor will not be instantiated.\n```\n\nWe can also try to unify this concept with `exportAs`, because the role and the local name should always match.\n## Proposal 2\n\nHave a generic mechanism for a directive to specify a compile-time predicate to determine if it should be instantiated.\n\n```\nfunction lastValueAccessor(thisBinding, bindings) {\n // find thisBinding in bindings, and make sure that this is the last value accessor.\n}\n\n@Directive({selector: '[ng-control]', predicate:lastValueAccessor)\nclass DefaultValueAccessor{}\n\n@Directive({selector: 'some custom stuff', predicate:lastValueAccessor)\nclass CustomValueAccessor{}\n```\n\nThis can be useful for other scenarios. For example, if you want to make sure that the dom structure matches some required shape. This may not work with Dart transformers.\n\n@mhevery @tbosch @jeffbcross \n","author":{"url":"https://github.com/vsavkin","@type":"Person","name":"vsavkin"},"datePublished":"2015-07-13T16:06:30Z","url":"https://github.com/angular/angular/issues/3011"}
| 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:7805fc76-463a-c4c4-27ee-9becdfbbe651 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9BFC:2AAD2E:1113ABF:16D3996:6B011B6A |
| html-safe-nonce | 6b6468c7217d33193803835f4d03b6afc531689ed779ef7c29a24bbc12b603b3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QkZDOjJBQUQyRToxMTEzQUJGOjE2RDM5OTY6NkIwMTFCNkEiLCJ2aXNpdG9yX2lkIjoiNDI1MzU2MDM5NDI2NzgyNzA1MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 285597c8086c37218ccff0a29129968c33d8b63c82e6270707ad1ddd5f2b31cf |
| hovercard-subject-tag | issue:94756419 |
| 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/3011/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4adaaafa491025ff5b10b989134686f6971265ce55955452ae89d61337e8f76e/angular/angular/issues/3011 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4adaaafa491025ff5b10b989134686f6971265ce55955452ae89d61337e8f76e/angular/angular/issues/3011 |
| og:image:alt | We need to provide a way to tell the compiler not to instantiate a directive when there is another, more specialized, directive that does the same job. Imagine we have a default value accessor dire... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vsavkin |
| 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