Accepted answer

The problem is definitely that you use'svg') or'app-d3-donut') in your custom component. is a wrapper around document.querySelector, which matches the first element it finds.

You need to make sure that this is not the first SVG element as seen from the document, but the first element as seen from the custom component. That is - you need to make sure that each Angular component can only look at and modify its own inner HTML.

From the source, also accepts HTML elements. So you should use Angular's ViewChild to get a the reference to the SVG element, and then pass that reference to d3.

  selector: 'app-d3-donut',
  template: `
    <svg #chartEl></svg>
class AppD3DonutComponent {
  @ViewChild('chartEl') svgElement: ElementRef;

  ngAfterViewInit() {'g') // etc

Related Query

More Query from same tag