In angular 2, svg-rect is a component which creates rect like below,
<svg height="550" width="450" x="0" y="0"> <g id="svgGroup"> <svg-rect> <!--template bindings={}--> <rect x="10" y="10" height="100" width="100" fill="red" stroke="#000" stroke-width="2"></rect> <!--template bindings={}--> </svg-rect> <svg-rect> <!--template bindings={}--> <rect x="10" y="10" height="100" width="100" fill="red" stroke="#000" stroke-width="2"></rect> <!--template bindings={}--> </svg-rect> </g> </svg>
but this won't render rect because of the special element tags created. If svg-rect tags are removed it renders the rect
<svg height="550" width="450" x="0" y="0"> <g id="svgGroup"> <!--template bindings={}--> <rect x="10" y="10" height="100" width="100" fill="red" stroke="#000" stroke-width="2"></rect> <!--template bindings={}--> <!--template bindings={}--> <rect x="10" y="10" height="100" width="100" fill="red" stroke="#000" stroke-width="2"></rect> <!--template bindings={}--> </g> </svg>
In Angular 1.x, there is replace: 'true' which removes the directive tags with the compiled output. Can we implement the same in angular2?
Every component is associated within an element that matches the component's selector. This element, into which the template is rendered, is called the host element. The :host pseudo-class selector may be used to create styles that target the host element itself, as opposed to targeting elements inside the host.
To turn an Angular component into something rendered in the DOM you have to associate an Angular component with a DOM element. We call such elements host elements. A component can interact with its host DOM element in the following ways: It can listen to its events.
As you can see, in this view's @Component() meta-data, there is no "selector" property. With routable components, this isn't required. But, it can be used.
In an Angular application, the "host context" selector is a way to define component styles based on some condition that exists outside of the current component. The Angular documentation describes the "host context" as looking for said condition higher-up in the DOM tree.
Instead of trying to get rid of the host element, turn it into one that is valid SVG but other wise unaffecting: Instead of your element selector
selector: "svg-rect"
and its corresponding element in the template:
template: `...<svg-rect>...</svg-rect>...`
switch to an attribute selector:
selector: "[svg-rect]"
and add that attribute to a group element tag:
template: `...<g svg-rect>...</g>...`
This will expand to:
<svg height="550" width="450" x="0" y="0"> <g id="svgGroup"> <g svg-rect> <!--template bindings={}--> <rect x="10" y="10" height="100" width="100" fill="red" stroke="#000" stroke-width="2"></rect> <!--template bindings={}--> </g> <g svg-rect> <!--template bindings={}--> <rect x="10" y="10" height="100" width="100" fill="red" stroke="#000" stroke-width="2"></rect> <!--template bindings={}--> </g> </g> </svg>
which is valid SVG, which will render. Plnkr
Another approach to remove the host of the component which I use.
Directive remove-host
//remove the host of avatar to be rendered as svg @Directive({ selector: '[remove-host]' }) class RemoveHost { constructor(private el: ElementRef) { } //wait for the component to render completely ngOnInit() { var nativeElement: HTMLElement = this.el.nativeElement, parentElement: HTMLElement = nativeElement.parentElement; // move all children out of the element while (nativeElement.firstChild) { parentElement.insertBefore(nativeElement.firstChild, nativeElement); } // remove the empty element(the host) parentElement.removeChild(nativeElement); } }
Using this directive;<avatar [name]="hero.name" remove-host></avatar>
In remove-host
directive, all the children of the nativeElement
are inserted before the host and then the host element is removed.
Sample Example Gist
Based on the use case, there might be a few performance issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With