question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SVG component rendered through ComponentFactoryResolver / ViewContainerRef doesn't show up

See original GitHub issue

I’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: image

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:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
alexzuzacommented, Jul 1, 2019

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

selector: 'svg:g[child]',

will be parsed to

<g child></g> not to <svg:g child></g>

As a result Angular will use document.createElement instead of document.createElementNS to create new element.

If we use selector like svg[child] then it will be parsed to <svg child> but for svg tag Angular provided namespace manually

https://github.com/angular/angular/blob/018477e2c4a31de69554c377ed25ba291636050a/packages/compiler/src/ml_parser/html_tags.ts#L85

Angular executes the following steps during html creation:

const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
document.body.appendChild(svg);

const comment = document.createComment('');
svg.appendChild(comment);

const g = document.createElementNS('http://www.w3.org/2000/svg', 'g');

const text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
text.setAttribute('x', '50%');
text.setAttribute('y', '50%');
g.appendChild(text);

const textNode = document.createTextNode('Hello');
text.appendChild(textNode);

svg.insertBefore(g, null);

https://jsfiddle.net/76x9kfm4/

But with only one difference

Instead of

const g = document.createElementNS('http://www.w3.org/2000/svg', 'g');

It uses

const g = document.createElement('g');

since there is no namespace provided.

As a result svg is not rendered correctly https://jsfiddle.net/w9syoape/

3reactions
dgrohcommented, Aug 31, 2018

any workaround while this is the milestone? this is crucial for our project.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found