Title: viewContainerRef.renderComponent should support hook on hostElement like createComponent function · Issue #51491 · angular/angular · GitHub
Open Graph Title: viewContainerRef.renderComponent should support hook on hostElement like createComponent function · Issue #51491 · angular/angular
X Title: viewContainerRef.renderComponent should support hook on hostElement like createComponent function · Issue #51491 · angular/angular
Description: Which @angular/* package(s) are relevant/related to the feature request? core Description hi @AndrewKushnir, look like ngx-dynamic-hooks work correctly and it using latest version of Angular core I just update. I tried to get rid of flic...
Open Graph Description: Which @angular/* package(s) are relevant/related to the feature request? core Description hi @AndrewKushnir, look like ngx-dynamic-hooks work correctly and it using latest version of Angular core I...
X Description: Which @angular/* package(s) are relevant/related to the feature request? core Description hi @AndrewKushnir, look like ngx-dynamic-hooks work correctly and it using latest version of Angular core I...
Opengraph URL: https://github.com/angular/angular/issues/51491
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"viewContainerRef.renderComponent should support hook on hostElement like createComponent function","articleBody":"### Which @angular/* package(s) are relevant/related to the feature request?\r\n\r\ncore\r\n\r\n### Description\r\n\r\nhi @AndrewKushnir, look like ngx-dynamic-hooks work correctly and it using latest version of Angular core I just update. I tried to get rid of flick and still experience that issue. I guess since they using renderer2 to render html direct instead of component render,so it cause this problem. So I decide do my own. look at the test file detail give me a lot of clue. But it not cover the case that component selector mix with normal html. \r\n\r\nhttps://github.com/AndrewKushnir/angular/commit/f9d02a7ced21a4569e667f85b05216a2ffe9e169#diff-af4c8e2af18bf92515f7cb47ed0e7a8be798116a9ca5c1e15f2ba4c3aa7b79b6\r\n\r\nWhich is the real world use case. For example I have a html code send from server:\r\n\r\n```\r\ncontent = `\r\n\u003ch1> hello world 1\u003c/h1> \r\n \u003cdynamic1> \u003c/dynamic1> \r\n \u003cdynamic2> \u003c/dynamic2> \r\n \u003ch1> hello world 2\u003c/h1>\r\n `;\r\n \r\n```\r\n## CONTEXT\r\n- I have a dictionary file to know tag-selector pair with which component.\r\n- I'm using RenderComponent below to render it and it work but it only able to render 1 component. and append it to the last of component as default. \r\n- I want to use selector as the hook and render component to that position but I'm confuse about this place. Now I have two option\r\n\r\n## TWO OPTION\r\n+ using angular _**createComponent() function from angular/core**_ to hook and create, then attachView as tutorial. It look like very useful and promising with hostElement to use as hook. But it just not working... https://angular.io/api/core/createComponent\r\n\r\n\r\n\r\n+ use _**vcr.createComponent**_ which is work geat. But it only append to last entry. See the index option parameter (index: the index at which to insert the new component's host view into this container. If not specified, appends the new view as the last entry). But this index is angular render level index instead of string position index. What should I do? How can I use that index? Or maybe we can have other solution, since there is no DOM on server to hook on. Maybe I can use renderer3 or something? \r\n\r\n+ since the htmlContent come from server, I can do some regex to mark on selector target like `\u003cdynamic #youShouldHookHere>\u003c/dynamic1>` or `\u003cng-content> \u003cdynamic1> \u003c/dynamic1> \u003cng-content>`\r\n\r\n```\r\n@Component({\r\n standalone: true,\r\n selector: 'dynamic1',\r\n template: `\r\n \u003cng-content />\r\n`,\r\n})\r\nclass Dynamic1Component {\r\n}\r\n\r\n@Component({\r\n standalone: true,\r\n selector: 'dynamic2',\r\n template: `\r\n \u003cng-content />\r\n`,\r\n})\r\nclass Dynamic2Component {\r\n}\r\n\r\n@Component({\r\n standalone: true,\r\n selector: 'app',\r\n template: `\u003cdiv [innerHTML]=\"content\">\u003c/div>`,\r\n})\r\nclass RenderComponent {\r\n content = `\u003ch1> hello world 1\u003c/h1> \r\n \u003cdynamic1> \u003c/dynamic1> \r\n \u003cdynamic2> \u003c/dynamic2> \r\n \u003ch1> hello world 2\u003c/h1>\r\n `;\r\n vcr!: ViewContainerRef;\r\n envInjector = inject(EnvironmentInjector);\r\n\r\n ngAfterViewInit() {\r\n // solution 1: this one work but only auto append at last\r\n const compRef = this.vcr.createComponent(Dynamic1Component);\r\n compRef.changeDetectorRef.detectChanges();\r\n \r\n // QUESTION: but how to render on dynamic1 hook? It will be perfect if we can give some instruction to hook on that to render....\r\n \r\n // solution 2: use application ref or use environmentInjector not work at all.\r\n const compRef = createComponent(Dynamic1Component, {\r\n // environmentInjector: this.envInjector, // not work\r\n environmentInjector: this.applicationRef.injector,\r\n });\r\n }\r\n}\r\n```\r\n\r\n\r\n### Proposed solution\r\n\r\nIf this one work, it can open a new world for SSR and hydration. Look at the insertImpl function I can see that index is the list of render command. Maybe we can add some renderId to mark on it?\r\n\r\n\r\n\r\n```\r\n@Component({\r\n standalone: true,\r\n selector: 'app',\r\n template: `\u003cmain>Hi! This is the main content.\u003cdynamic>\u003c/dynamic> \u003c/main>`,\r\n})\r\nclass AppComponent {\r\n vcr = inject(ViewContainerRef);\r\n\r\n ngAfterViewInit() {\r\n const compRef = this.vcr.createComponent(DynamicComponent, { \r\n hostElement: // propose 1: this one will look like createComponent function,use DOCUMENT in angular/common support by domnio on server \r\n selector: 'dynamic', // propose 2: this solution, so domino will search for hostElement use querySelectorAll\r\n stringReplace: '\u003cdynamic>\u003c/dynamic>' // propose 3: let developer parse job, angular just do string replace\r\n } );\r\n compRef.changeDetectorRef.detectChanges();\r\n }\r\n}\r\n ```\r\n\r\n### Alternatives considered\r\n\r\nThank for your support, I will close this issue once I found the necessary suggestion. \r\n\r\nI'm thinking of recursion component. It will try to render it self. For example:\r\n```\r\n content = `\u003ch1> hello world 1\u003c/h1> \r\n \u003cdynamic1> \u003c/dynamic1> \r\n \u003cdynamic2> \u003c/dynamic2> \r\n \u003ch1> hello world 2\u003c/h1>\r\n `;\r\n```\r\n\r\nwill be convert to:\r\n\r\n```\r\n content = `\u003ch1> hello world 1\u003c/h1> \r\n \u003crecursion-component [content]=\"'\u003cdynamic1> \u003c\\/dynamic1>'\"> \u003c/recursion-component> \r\n \u003crecursion-component [content]=\"'\u003cdynamic1> \u003c\\/dynamic1>'\"> \u003c/recursion-component> \r\n \u003ch1> hello world 2\u003c/h1>\r\n `;\r\n ```\r\n \r\n So in theory, we only render one and only component `recursion-component` it will detect if it this component is only single component selector content or mix html content and decide to render or continue the recursion loop. \r\n \r\n \r\n ","author":{"url":"https://github.com/hiepxanh","@type":"Person","name":"hiepxanh"},"datePublished":"2023-08-24T08:41:25Z","url":"https://github.com/angular/angular/issues/51491"}
| 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:31720b40-f801-db9f-1e00-d751508946cc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 95E2:291B58:B49DD0:E402FB:6B013131 |
| html-safe-nonce | 8e42cae4f0f35f40e54bc282ac92cc933d19c3f49219932b981e154c8eb61f99 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NUUyOjI5MUI1ODpCNDlERDA6RTQwMkZCOjZCMDEzMTMxIiwidmlzaXRvcl9pZCI6Ijg3NDA4MTU5MDU4NDU0ODk5NjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8116226757688e992ec4c9fda653f60c19b8ded431d7ecbd90b051d2c410df72 |
| hovercard-subject-tag | issue:1864687050 |
| 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/51491/issue_layout |
| twitter:image | https://opengraph.githubassets.com/67102e583dc162240af8645f1e5e6f1dddcbc0618fd5066b34bd43ab4f635baf/angular/angular/issues/51491 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/67102e583dc162240af8645f1e5e6f1dddcbc0618fd5066b34bd43ab4f635baf/angular/angular/issues/51491 |
| og:image:alt | Which @angular/* package(s) are relevant/related to the feature request? core Description hi @AndrewKushnir, look like ngx-dynamic-hooks work correctly and it using latest version of Angular core I... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | hiepxanh |
| 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