Title: Proposal: optional namespaces for inputs bindings · Issue #12557 · angular/angular · GitHub
Open Graph Title: Proposal: optional namespaces for inputs bindings · Issue #12557 · angular/angular
X Title: Proposal: optional namespaces for inputs bindings · Issue #12557 · angular/angular
Description: I'm submitting a ... bug report => search github for a similar issue or PR before submitting feature request support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIB...
Open Graph Description: I'm submitting a ... bug report => search github for a similar issue or PR before submitting feature request support request => Please do not submit support request here, instead see https://github...
X Description: I'm submitting a ... bug report => search github for a similar issue or PR before submitting feature request support request => Please do not submit support request here, instead see http...
Opengraph URL: https://github.com/angular/angular/issues/12557
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: optional namespaces for inputs bindings","articleBody":"\u003c!--\nIF YOU DON'T FILL OUT THE FOLLOWING INFORMATION WE MIGHT CLOSE YOUR ISSUE WITHOUT INVESTIGATING\n-->\n\n**I'm submitting a ...**\n- [ ] bug report => search github for a similar issue or PR before submitting\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## Problem\n\nTechnically, it's totally possible that there are multiple directives on the same element that bind to the same property (name). Currently there's no way to explicitly tell Angular which directive property we want to bind to. \n\n_This is especially problematic if the consumer doesn't have control over the directive (e.g. 3rd party directives)_\n\nExample:\n\n```\n@Directive({\n selector: '[one]'\n})\nclass One {\n @Input() foo;\n}\n\n@Directive({\n selector: '[two]'\n})\nclass Two {\n @Input() foo;\n}\n```\n\nThose two directives can be used like this:\n\n```\n\u003cANY one two [foo]=\"someExpr\">\u003c/ANY>\n```\n\nBy looking at the template we don't know which directive we're actually binding to + we can't be explicit about it either.\n### Current behaviour\n\nThe code above binds `someExpr` to both directive `foo` properties.\n## Proposals\n\nI discussed this with @cburgdorf and we came up with the following **possible** solutions, both with their up and downsides.\n### 1. Directive-Attribute/Property order defines binding receivers\n\nThe directive-attribute and property order on the host element defines which expression is bound to which directive prop. Example:\n\n```\n\u003cdiv one [foo]=\"boundToOne\" two [foo]=\"boundToTwo\">\u003c/div>\n```\n\nSame for\n\n```\n\u003cdiv one two [foo]=\"boundToOne\" [foo]=\"boundToTwo\">\u003c/div>\n```\n\n^ This works, because Angular has its own HTML parser and can actually identify the order of attributes (which browsers can’t guarantee AFAIK)\n#### Upsides\n- Easy to grok\n- Kinda intuitive\n#### Downsides\n- Gets hairy the more directives are applied to one host element (Chances might be rather low, that this happens with many directives tho)\n### 2. Introduce optional namespace for property bindings\n\nAnother option would be to introduce some explicit namespaces (maybe by taking advantage of `exportAs`?)\n\nAssuming:\n\n```\n@Directive({\n selector: '[one]',\n exportAs: 'one'\n})\nclass One {\n @Input() foo;\n}\n\n@Directive({\n selector: '[two]',\n exportAs: 'two'\n})\nclass Two {\n @Input() foo;\n}\n```\n\n^ Notice how both directives use `exportAs`. That name could be used by the consumer as well:\n\n```\n\u003cdiv one [one.foo]=\"boundToOne\" two [two.foo]=\"boundToTwo\">\u003c/div>\n```\n\nThis would work in any order:\n\n```\n\u003cdiv [one.foo]=\"…\" two one [two.foo]=\"…\">\n```\n\nAnother possible syntax would be\n\n```\n\u003cdiv [one#foo]=\"…\">\n```\n\nCanonical version:\n\n```\n\u003cdiv bind-one-foo=\"…\"\n```\n\nThis works for events too.\n#### Possible problems with this approach\n\nOne problem it would introduce though (if we’d rely on `exportAs`), the consumer also doesn’t have control over `exportAs` values of 3rd party directives either. So if we have:\n\n```\n@Directive({\n selector: '[one]',\n exportAs: 'one'\n})\nexport class One {…}\n\n@Directive({\n selector: '[two]',\n exportAs: 'one'\n})\nexport class Two {…}\n```\n\n^ Notice how both directives `exportAs: 'one'`\n\nThis would kind of result in the same problem, as both directives are exported as `one`. However, OTOH this exact same problem exists already when getting directive instance references in the template:\n\n```\n\u003cdiv one two #foo=\"one\">\u003c/div>\n```\n\n^ here, we can’t say if `foo` is `One` or `Two`. In fact, Angular throws in that case\n\n```\nzone.js:344Unhandled Promise rejection: Template parse errors:\nReference \"#foo\" is defined several times (\"\u003cdiv one two [ERROR ->]#foo=\"one\">\u003c/div>\n\"):\n```\n### Final thought\n\nSo then, I’d say this boils down to always prefixing the `exportAs` value, then this might work. E.g. forms directives do the exactly the same (`ngModel`, `ngForm`, ...)\n","author":{"url":"https://github.com/0x-r4bbit","@type":"Person","name":"0x-r4bbit"},"datePublished":"2016-10-27T08:31:23Z","url":"https://github.com/angular/angular/issues/12557"}
| 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:ecc3102b-4400-ca45-88fd-8fe7620fc1be |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 89FC:22DB35:11889E7:1685FE9:6B010A60 |
| html-safe-nonce | 5be0cbf46f8137de7575b30b0e69d5ab4d8ee5667b3b846eb1fbb5e3be4866fc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OUZDOjIyREIzNToxMTg4OUU3OjE2ODVGRTk6NkIwMTBBNjAiLCJ2aXNpdG9yX2lkIjoiMTA0OTMwMzkyMTQ0MjQ5MDk3NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f0ac2055d6ca7305b608984fdef557ae2e42a4e58887a11518f8434128e1c278 |
| hovercard-subject-tag | issue:185604832 |
| 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/12557/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7fab2e6a65f2320d77e3ee11b7f68433fcde46ccaea3cc89e2e3228a9bd41e28/angular/angular/issues/12557 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7fab2e6a65f2320d77e3ee11b7f68433fcde46ccaea3cc89e2e3228a9bd41e28/angular/angular/issues/12557 |
| og:image:alt | I'm submitting a ... bug report => search github for a similar issue or PR before submitting feature request support request => Please do not submit support request here, instead see https://github... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | 0x-r4bbit |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width