Title: Support for dynamic Angular Elements / Webcomponents (with Renderers) · Issue #35328 · angular/angular · GitHub
Open Graph Title: Support for dynamic Angular Elements / Webcomponents (with Renderers) · Issue #35328 · angular/angular
X Title: Support for dynamic Angular Elements / Webcomponents (with Renderers) · Issue #35328 · angular/angular
Description: 🚀 feature request Relevant Package The functionality relates to the @angular/platform-browser package as this is the package which implements DomRendererFactory2 and DefaultDomRenderer2 Description Angular Elements and Webcomponents in g...
Open Graph Description: 🚀 feature request Relevant Package The functionality relates to the @angular/platform-browser package as this is the package which implements DomRendererFactory2 and DefaultDomRenderer2 Description...
X Description: 🚀 feature request Relevant Package The functionality relates to the @angular/platform-browser package as this is the package which implements DomRendererFactory2 and DefaultDomRenderer2 Description...
Opengraph URL: https://github.com/angular/angular/issues/35328
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support for dynamic Angular Elements / Webcomponents (with Renderers)","articleBody":"# 🚀 feature request\r\n\r\n### Relevant Package\r\nThe functionality relates to the `@angular/platform-browser` package as this is the package which implements `DomRendererFactory2` and `DefaultDomRenderer2`\r\n\r\n\r\n### Description\r\nAngular Elements and Webcomponents in general represent great solution for various [real world use cases](https://angular-extensions.github.io/elements/#/docs/use-cases#sub-applications) like...\r\n* sub-applications - ability to deploy library independently which will update all consumers without need to rebuild these consumer SPAs because lib is only referenced by url (as Angular Element bundle)\r\n* microfrontends ( runtime-configurable )\r\n\r\nUsual approach to consuming Angular Elements and Web components leaves us with 2 options:\r\n1. eager loading + standard Angular template binding `\u003csome-element [prop]=\"value\">`\r\n2. lazy loading + imperative component creation and props / event binding `document.createElement('some-element');` ...\r\n\r\nThe library `@angular-extensions/elements` solves this by enabling both **lazy loading** and **standard Angular template binding** `\u003csome-element *axLazyElement=\"bundle.js\" [prop]=\"value\">` and this works with both ViewEngine and IVY...\r\n\r\n### Dynamic element use case\r\nIn previous section we're committing to element being `\u003csome-element>` by hard-coding it into template of the consumer Angular component. What if we wanted to support loading element configuration at runtime (eg from backend) to enable fully dynamic microfrontends?\r\n\r\nAs it turns out this works too (at least for the ViewEngine) so it is possible to write `\u003cax-lazy-element *axLazyElementDynamic=\"'some-element'; url: 'bundle.js'\" [prop]=\"value\">\u003c/ax-lazy-element>`.\r\nThe bundle will be lazy loaded, and then the element will be rendered as `\u003csome-element>`.\r\nThe only thing needed to do that is to override value of the `tagName` in the `TemplateRef` of the `*axLazyElemendDynamic` directive and Angular will render desired element supporting our use case.\r\n\r\nNow this does NOT work with IVY since in IVY template is an function. \r\nRob @robwormald gave me hint that it should be possible to override `Renderer` (or use custom) to hook into rendering and override element there.\r\n\r\n### The Issue\r\nIt is possible to achieve that with render BUT:\r\n* currently both `DomRendererFactory2` and `DefaultDomRenderer2` are private so it is NOT possible to extend them and hence I would need to re-implement the whole renderer which sounds pretty excessive and hard to maintain\r\n* PLUS as far as I am aware the render API changed [older NRWL article](https://blog.nrwl.io/experiments-with-angular-renderers-c5f647d4fd9e) so there is no more `RootRenderer` ?\r\n\r\n### Current [solution](https://github.com/angular-extensions/elements/blob/master/projects/elements/src/lib/lazy-elements/lazy-element-dynamic/lazy-element-dynamic.directive.ts#L71-L80)\r\nPlay around with **live demo** (which serves both lib and demo and rebuilds on changes to any of them) using this one liner `git clone https://github.com/angular-extensions/elements.git elements && cd elements && npm ci && npm start` once it runs, please navigate to `Examples > Dynamic` to see it in action.\r\n\r\n\r\n```typescript\r\n@Directive({\r\n selector: '[axLazyElementDynamic]'\r\n})\r\nexport class LazyElementDynamicDirective implements OnInit {\r\n @Input('axLazyElementDynamic') tag: string;\r\n @Input('axLazyElementDynamicUrl') url: string;\r\n\r\n constructor(\r\n @Inject(DOCUMENT) private document: Document,\r\n private renderer: Renderer2,\r\n private cdr: ChangeDetectorRef,\r\n private template: TemplateRef\u003cany>,\r\n private elementsLoaderService: LazyElementsLoaderService\r\n ) {}\r\n\r\n ngOnInit() {\r\n this.elementsLoaderService\r\n .loadElement(this.url)\r\n .then(() => {\r\n this.vcr.clear();\r\n const originalCreateElement = this.renderer.createElement;\r\n this.renderer.createElement = (name: string, namespace: string) => { // temporary override \r\n if (name === 'ax-lazy-element') {\r\n name = this.tag;\r\n }\r\n return this.document.createElement(name);\r\n };\r\n this.vcr.createEmbeddedView(this.template);\r\n this.renderer.createElement = originalCreateElement;\r\n this.cdr.markForCheck();\r\n })\r\n }\r\n}\r\n```\r\n \r\n\r\n\r\n### Describe the solution you'd like\r\nProbably being able to easily extend default renderer and being able to register it like we're used to with interceptors or control value accessors.\r\n\r\nI would like to give people option (or do it for them behind the scenes) to do\r\n```typescript\r\n@NgModule({\r\n import: [LazyElementsModule],\r\n providers: [{ provide: Renderer, useClass: LazyElementsRenderer }]\r\n})\r\nexport class AppModule {}\r\n\r\nexport class LazyElementsRenderer extends DefaultDomRenderer2 {\r\n \r\n constructor(...args) {\r\n this.super(...args);\r\n }\r\n\r\n createElement() {\r\n // override logic and change tag name if necessary\r\n }\r\n}\r\n```\r\n\r\n\r\n### Describe alternatives you've considered\r\nI spent quite some time trying to override tag inside of template function without success \r\n","author":{"url":"https://github.com/tomastrajan","@type":"Person","name":"tomastrajan"},"datePublished":"2020-02-11T09:33:50Z","url":"https://github.com/angular/angular/issues/35328"}
| 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:c167493b-f2fb-9365-b4a2-21a2f8603267 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF7E:29FFA9:166EFC8:1C71A24:6B0133C5 |
| html-safe-nonce | 282796e40a4407c8f3aa7e19e5a705b3cc335dc90204b800c8b7f8d581b543ab |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjdFOjI5RkZBOToxNjZFRkM4OjFDNzFBMjQ6NkIwMTMzQzUiLCJ2aXNpdG9yX2lkIjoiNDY0MzIwMjE5ODE4MjkwMDY3NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2ba4cf61c53bbf52a2d7954454b9be047c14663964f1cdde54f6dfa7f1baeb71 |
| hovercard-subject-tag | issue:563075887 |
| 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/35328/issue_layout |
| twitter:image | https://opengraph.githubassets.com/90b04e8916eb0cddd604802448480acd6236946ad5d5bd0e8b645ee9bca14202/angular/angular/issues/35328 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/90b04e8916eb0cddd604802448480acd6236946ad5d5bd0e8b645ee9bca14202/angular/angular/issues/35328 |
| og:image:alt | 🚀 feature request Relevant Package The functionality relates to the @angular/platform-browser package as this is the package which implements DomRendererFactory2 and DefaultDomRenderer2 Description... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tomastrajan |
| 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