Title: [feature] Ability to clone a TemplateRef to multiple outlets at once · Issue #37995 · angular/angular · GitHub
Open Graph Title: [feature] Ability to clone a TemplateRef to multiple outlets at once · Issue #37995 · angular/angular
X Title: [feature] Ability to clone a TemplateRef to multiple outlets at once · Issue #37995 · angular/angular
Description: 🚀 Ability to clone a TemplateRef to multiple outlets at once Relevant Package I could see this being a useful utility in @angular/cdk, but would likely require an update to @angular/core. I'm honestly not sure. Description Essentially, I...
Open Graph Description: 🚀 Ability to clone a TemplateRef to multiple outlets at once Relevant Package I could see this being a useful utility in @angular/cdk, but would likely require an update to @angular/core. I'm hones...
X Description: 🚀 Ability to clone a TemplateRef to multiple outlets at once Relevant Package I could see this being a useful utility in @angular/cdk, but would likely require an update to @angular/core. I'm h...
Opengraph URL: https://github.com/angular/angular/issues/37995
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[feature] Ability to clone a TemplateRef to multiple outlets at once","articleBody":"# 🚀 Ability to clone a TemplateRef to multiple outlets at once\r\n\r\n### Relevant Package\r\nI could see this being a useful utility in `@angular/cdk`, but would likely require an update to `@angular/core`. I'm honestly not sure.\r\n\r\n\r\n### Description\r\nEssentially, I would like to have the ability to take a `TemplateRef` and inject _copies_ of it into multiple template outlets. The use case that made me aware of this limitation was trying to create a `\u003cselect>`-style form control, where consumers could put any arbitrary content into an option (not just text, but icons, images, components, etc.). When an option is selected, the Select component would _re_-project that option's content into the Select's template to indicate the currently selected option.\r\n\r\nIn case that's not clear, here's a simplified/truncated version of the solution that I attempted.\r\n\r\n```typescript\r\n@Component({\r\n selector: 'my-select',\r\n template: `\r\n \u003cdiv class=\"selected-value\">\r\n \u003cng-container *ngTemplateOutlet=\"selectedTemplate\">\u003c/ng-container>\r\n \u003cng-container *ngIf=\"!selectedTemplate\">{{ placeholder }}\u003c/ng-container>\r\n \u003c/div>\r\n \u003cdiv class=\"options\">\r\n \u003cng-content>\u003c/ng-content>\r\n \u003c/div>\r\n `\r\n})\r\nexport class SelectComponent implements AfterContentInit {\r\n\r\n @Input() placeholder = \"Select an option\";\r\n @ContentChildren(OptionComponent) options: QueryList\u003cOptionComponent>;\r\n selectedTemplate: TemplateRef\u003cnull>;\r\n\r\n ngAfterContentInit() {\r\n this.options.forEach(option => {\r\n option.selected.subscribe(() => {\r\n this.selectedTemplate = option.contentTemplate;\r\n });\r\n });\r\n }\r\n\r\n}\r\n\r\n@Component({\r\n selector: 'my-option',\r\n template: `\r\n \u003cng-container *ngTemplateOutlet=\"contentTemplate\">\u003c/ng-container>\r\n \u003cng-template #contentTemplate>\r\n \u003cng-content>\u003c/ng-content>\r\n \u003c/ng-template>\r\n `\r\n})\r\nexport class OptionComponent {\r\n\r\n @Output() selected = new EventEmitter\u003cvoid>();\r\n @ViewChild('contentTemplate') contentTemplate: TemplateRef\u003cnull>;\r\n\r\n @HostListener('click') onClick() {\r\n this.selected.emit();\r\n }\r\n\r\n}\r\n```\r\n\r\nAnd here's an example usage that takes advantage of the template cloning:\r\n\r\n```html\r\n\u003clabel for=\"status\">Task Status\u003c/label>\r\n\u003cmy-select id=\"status\" [(ngModel)]=\"statusValue\">\r\n \u003cmy-option [value]=\"Status.Blocked\">\r\n \u003cmy-icon icon=\"ban\">\u003c/my-icon> Blocked\r\n \u003c/my-option>\r\n \u003cmy-option [value]=\"Status.Researching\">\r\n \u003cmy-icon icon=\"science\">\u003c/my-icon> Researching\r\n \u003c/my-option>\r\n \u003cmy-option [value]=\"Status.InProgress\">\r\n \u003cmy-icon icon=\"ellipsis\">\u003c/my-icon> In Progress\r\n \u003cmy-option>\r\n \u003cmy-option [value]=\"Status.Done\">\r\n \u003cmy-icon icon=\"check\">\u003c/my-icon> Done\r\n \u003c/my-option>\r\n\u003c/my-select>\r\n```\r\n\r\nThis works as intended, except it appears that the `TemplateRef` can only be embedded into one view container at any given time. So, when an option is selected, it appears in the SelectComponent's template outlet (🎉), but _disappears_ from the OptionComponent's template outlet (😭), leaving a blank spot in the options list.\r\n\r\n\r\n\r\n\r\n### Describe the solution you'd like\r\nIt would be great if the sample code above just worked as expected out of the box, but I can understand if that's not feasible, or if it's undesirable for some reason I'm not aware of.\r\n\r\nI dug through some of CDK's source code, because I knew that its Drag-and-Drop feature was doing some sort of element cloning for the preview and placeholder elements, so I hoped that maybe they were making use of some Angular template/view APIs that I was unfamiliar with. Unfortunately, it looks like that feature is just using some [raw, low-level DOM manipulation](https://github.com/angular/components/blob/fb769ef421f5b73a5d88163864e89e1b834086ec/src/cdk/drag-drop/drag-ref.ts#L1316) to achieve this, which would be way overkill for my use case.\r\n\r\nI wonder if it would be possible to add a `clone` method to the `TemplateRef` class, which would return a new `TemplateRef` that could be embedded into an additional template outlet without detaching the original?\r\n\r\n\r\n### Describe alternatives you've considered\r\n\r\nIf I specifically wanted to enable icons in my OptionComponent, of course I could add an `@Input() icon: IconName`, and replicate that to my SelectComponent. I could make the OptionComponent's label a simple `@Input label: string` while I'm at it, instead of fiddling with the content projection at all. But I like to keep these types of core UI components as flexible as possible for a wide variety of use cases, so that when a new use case comes up that I hadn't originally anticipated, it can be accommodated without needing to make major updates to the component or [endlessly tack on configuration options](https://youtu.be/0BikjL858OQ?t=324). If there's another alternative or workaround that I haven't thought of, I would love to hear it.","author":{"url":"https://github.com/dannymcgee","@type":"Person","name":"dannymcgee"},"datePublished":"2020-07-09T17:44:31Z","url":"https://github.com/angular/angular/issues/37995"}
| 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:efa61bc8-61f7-0ab0-060f-a593b7330eda |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A862:237673:146B1A4:1B4286E:6B0130F6 |
| html-safe-nonce | ee678c7cceba5b5ac3c307e2e5941bd18398fdb0a5e4f740246fd6032b08b9ec |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBODYyOjIzNzY3MzoxNDZCMUE0OjFCNDI4NkU6NkIwMTMwRjYiLCJ2aXNpdG9yX2lkIjoiNzY3OTI2Mzg0MzI3NjYzMjMxMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 86a3b995b41ec20267f8d93724b43f66dd3093e3dc87a31f2d2e792c9b66a89d |
| hovercard-subject-tag | issue:654237939 |
| 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/37995/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d0272e958e6dc2b9d2fafe0018d905fe20a8092862b5324ba69abe2b1529f39e/angular/angular/issues/37995 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d0272e958e6dc2b9d2fafe0018d905fe20a8092862b5324ba69abe2b1529f39e/angular/angular/issues/37995 |
| og:image:alt | 🚀 Ability to clone a TemplateRef to multiple outlets at once Relevant Package I could see this being a useful utility in @angular/cdk, but would likely require an update to @angular/core. I'm hones... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dannymcgee |
| 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