【发布时间】:2017-10-21 20:03:49
【问题描述】:
我创建了一个结构指令,当我将鼠标悬停在“查看工具提示”文本上时,该指令根据 ng 模板中的内容显示工具提示 工具提示显示正确,但显示在屏幕的顶部:0px 左侧:0px 位置,我希望它显示在文本“查看工具提示”的正上方,我已经使用方法“实现了 elementRef 的尺寸” getBoundingClientRect()" 但我不知道如何在工具提示中应用它们。有什么想法吗?
tooltip.directive.ts
import { Component, Input, HostListener, Directive, ElementRef,
TemplateRef, ViewContainerRef, ContentChild, ComponentRef } from
'@angular/core';
@Directive({ selector: '[tooltipDirective]' })
export class TooltipDirective {
private tooltipId: string;
private dimensiones:{};
constructor(private elementRef: ElementRef,
private viewContainerRef: ViewContainerRef) { }
@Input() parametroPlantilla: TemplateRef<any>;
@ContentChild( "tooltipTemplate" ) private tooltipTemplateRef: TemplateRef
<Object>;
@HostListener('mouseenter') onMouseEnter(): void {
this.viewContainerRef.createEmbeddedView(this.tooltipTemplateRef);
this.dimensiones = this.elementRef.nativeElement.getBoundingClientRect();
}
@HostListener('mouseleave') onMouseLeave(): void {
if (this.viewContainerRef) {
this.viewContainerRef.clear();
}
}
}
display.component.ts
...Some stuff
<div tooltipDirective>See tooltip!
<ng-template #tooltipTemplate >
<div>
This is my tooltip!
</div>
</ng-template>
</div>
【问题讨论】:
标签: angular tooltip ng-template