SVG component rendered through ComponentFactoryResolver / ViewContainerRef doesn't show up
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
When I dynamically render an SVG component through ComponentFactoryResolver into a ViewContainerRef, the browser doesn’t render anything.
Expected behavior
Dynamically rendered SVG components should be visible.
Minimal reproduction of the problem with instructions
http://plnkr.co/edit/qAOHEmlQFuXjlUwAojJF?p=preview
There’s only one red square when the SVG component is rendered with a component selector.
There’s no square when the SVG component is dynamically instantiated.
Looking at the DOM, the only difference is the comment tag from the ng-template
:
What is the motivation / use case for changing the behavior?
Environment
Angular version: 5.0.[01]
Browser:
- [x ] Chrome (desktop) version XX
- [? ] Chrome (Android) version XX
- [? ] Chrome (iOS) version XX
- [? ] Firefox version XX
- [? ] Safari (desktop) version XX
- [? ] Safari (iOS) version XX
- [? ] IE version XX
- [x ] Edge version XX
For Tooling issues:
- Node version: XX
- Platform:
Others:
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:13 (3 by maintainers)
Top Results From Across the Web
html - Angular: Using ComponentFactoryResolver for dynamic ...
Your selector doesn't contain svg namespace. In order to render it correctly the selector should be svg:g[hello] .
Read more >ViewContainerRef - Angular
Anchor element that specifies the location of this container in the containing view. Each view container can have only one anchor element, and...
Read more >Dynamically add components to the DOM with Angular - Medium
I'll focus in the use of the ViewContainerRef to attach a component. This object is basically a $(<selector>) for Angular (DOM manipulation). Injecting...
Read more >Dynamic Components - Angular - w3resource
The pain of building dynamic component is greatly reduced in Angular, as it comes with its own API for loading components dynamically. The ......
Read more >Integrating Angular components into D3 : r/Angular2 - Reddit
It is using Angular to render and animate the SVG elements with all of ... dynamically using ComponentFactoryResolver at a ViewContainerRef.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The main problem here is that Angular selector can’t contain namespace for creating element https://github.com/angular/angular/blob/f8096d499324cf0961f092944bbaedd05364eea1/packages/compiler/src/selector.ts#L11-L21
The following selector
will be parsed to
<g child></g>
not to<svg:g child></g>
As a result Angular will use
document.createElement
instead ofdocument.createElementNS
to create new element.If we use selector like
svg[child]
then it will be parsed to<svg child>
but forsvg
tag Angular provided namespace manuallyhttps://github.com/angular/angular/blob/018477e2c4a31de69554c377ed25ba291636050a/packages/compiler/src/ml_parser/html_tags.ts#L85
Angular executes the following steps during html creation:
https://jsfiddle.net/76x9kfm4/
But with only one difference
Instead of
It uses
since there is no namespace provided.
As a result svg is not rendered correctly https://jsfiddle.net/w9syoape/
any workaround while this is the milestone? this is crucial for our project.