Title: [feature] NgComponentOutlet · Issue #11168 · angular/angular · GitHub
Open Graph Title: [feature] NgComponentOutlet · Issue #11168 · angular/angular
X Title: [feature] NgComponentOutlet · Issue #11168 · angular/angular
Description: I'm submitting a ... (check one with "x") [ ] bug report => search github for a similar issue or PR before submitting [ x ] feature request [ ] support request => Please do not submit support request here, instead see https://github.com/...
Open Graph Description: I'm submitting a ... (check one with "x") [ ] bug report => search github for a similar issue or PR before submitting [ x ] feature request [ ] support request => Please do not submit support reque...
X Description: I'm submitting a ... (check one with "x") [ ] bug report => search github for a similar issue or PR before submitting [ x ] feature request [ ] support request => Please do not ...
Opengraph URL: https://github.com/angular/angular/issues/11168
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[feature] NgComponentOutlet","articleBody":"**I'm submitting a ...** (check one with \"x\")\n\n```\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```\n\n**Current behavior**\nNone\n\n**Expected/desired behavior**\nA directive that is able to create components dynamically, the same way as `NgTemplateOutlet` created embedded view dynamically.\n#### Pros\n- Remove the boilerplate in a component. Developer does not need to query (`ViewChild`) and manually create the component. (`ComponentFactoryResolver`, `ViewContainerRef`)\n- Expressed in the template.\n- Simple\n- Aligned with NgTemplateOutlet, same idea same concept\n#### Cons\n- Host attribute bindings are not expressed in a template. Not much of a con, as its expected and `ComponentRef` can be used to set values on the instance...\n- Getting a reference to the returned `ComponentRef` is via callback only.\n- more ?\n\n**Directive**\n\n``` ts\n@Directive({\n selector: '[swapCmp]'\n})\nexport class SwapComponentDirective {\n private component: any;\n\n constructor(private cfr: ComponentFactoryResolver,\n private vcRef: ViewContainerRef,\n private tRef: TemplateRef\u003cObject>) {\n }\n\n @Input() swapCmpBindings: ResolvedReflectiveProvider[];\n @Input() swapCmpInjector: Injector;\n @Input() swapCmpProjectables: any[][];\n\n @Output() onCreate: EventEmitter\u003cComponentRef\u003cany>> = new EventEmitter\u003cComponentRef\u003cany>>(false);\n\n @Input() set swapCmp(component: any) {\n this.component = component;\n this.vcRef.clear();\n if (this.component) {\n let injector = this.swapCmpInjector || this.vcRef.parentInjector;\n\n if (Array.isArray(this.swapCmpBindings) && this.swapCmpBindings.length > 0) {\n injector = ReflectiveInjector.fromResolvedProviders(this.swapCmpBindings, injector);\n }\n\n const cmpRef = this.vcRef.createComponent(\n this.cfr.resolveComponentFactory(component),\n this.vcRef.length,\n injector,\n this.swapCmpProjectables\n );\n\n cmpRef.changeDetectorRef.detectChanges();\n\n this.onCreate.emit(cmpRef);\n }\n }\n}\n```\n\n```\n \u003ctemplate [swapCmp]=\"ctx.component\" [swapCmpBindings]=\"ctx.bindings\" [swapCmpInjector]=\"ctx.injector\" [swapCmpProjectables]=\"ctx.projectableNodes\">\u003c/template>\n```\n\n> The name here is SwapComponent but it just as an example, took it from my [code base](https://github.com/shlomiassaf/angular2-modal/blob/master/src/components/angular2-modal/components/swap-component.directive.ts)\n- A Developer provides a component, changing the component will re-create.\n- Injector is optional, if not supplied the current injector is used.\n- Bindings are used to allow additional providers to the injector (something like `viewProviders`)\n- In case `ng-content` is needed the developer can supply projectableNodes.\n\nWhat do you think?\n","author":{"url":"https://github.com/shlomiassaf","@type":"Person","name":"shlomiassaf"},"datePublished":"2016-08-30T18:51:15Z","url":"https://github.com/angular/angular/issues/11168"}
| 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:51e863ce-4e8b-ffcb-3743-70357848d8db |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D016:76F02:F87E53:13FC3A3:6B010351 |
| html-safe-nonce | f150c1ad5906af386d266eed6fb5c2c86d9198a741a3755e7eec1cca9478df97 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMDE2Ojc2RjAyOkY4N0U1MzoxM0ZDM0EzOjZCMDEwMzUxIiwidmlzaXRvcl9pZCI6IjMxMjE4MTQyMTIzMDE2MjAwNDkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3236c9e629e162006440e9c34ec659e287a5c39bcd694678258924f26c374b18 |
| hovercard-subject-tag | issue:174096053 |
| 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/11168/issue_layout |
| twitter:image | https://opengraph.githubassets.com/436ee12ea894195108e6ef4c7df7e9c8c2d3d97d585e882ecbae9eabe540cf63/angular/angular/issues/11168 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/436ee12ea894195108e6ef4c7df7e9c8c2d3d97d585e882ecbae9eabe540cf63/angular/angular/issues/11168 |
| og:image:alt | I'm submitting a ... (check one with "x") [ ] bug report => search github for a similar issue or PR before submitting [ x ] feature request [ ] support request => Please do not submit support reque... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | shlomiassaf |
| 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