【发布时间】:2019-09-27 14:07:29
【问题描述】:
当我悬停一个 svg 元素时应该显示一个工具提示。
我希望工具提示是一个角度组件,以便于创建 UI。
限制是 svg 元素是以编程方式创建的,所以我不知道如何为动态组件渲染创建模板引用。
我的问题是如何构建一个角度组件作为动态 svg 元素的工具提示?
这是 stackblitz 上的 sample。我写了动态组件的代码。现在我应该在悬停圆圈时动态渲染它。
@ViewChild('cont', { static:true,read: ViewContainerRef }) entry: ViewContainerRef;
constructor(private resolver: ComponentFactoryResolver) { }
@ViewChild('svg', { static: true }) svg: ElementRef
ngAfterViewInit() {
let circle= document.createElementNS('http://www.w3.org/2000/svg','circle')
circle.setAttribute('r','40');
circle.setAttribute('cx','50');
circle.setAttribute('cy','50');
//let cont= document.createAttributeNS('http://www.w3.org/2000/svg','#cont')
//circle.setAttribute('#cont','test')
this.svg.nativeElement.appendChild(circle)
this.entry.clear();
const factory = this.resolver.resolveComponentFactory(TooltipComponent);
const componentRef = this.entry.createComponent(factory);
}
【问题讨论】:
标签: javascript angular