【发布时间】:2020-11-12 16:35:55
【问题描述】:
我创建了一个使用可变长度数组填充工具提示的指令。这很好用,但我需要动态设置工具提示的样式,以便它保留在初始触发器组件下。使用根据项目数量变化的顶部或底部值。
<div tooltipDirective [tooltipDataArray]="['Person1', 'Person2', 'Person3', 'Person4', 'Person5', 'Person6']">See tooltip!
<ng-template #tooltipTemplate >
<div class="tooltip" [ngStyle]="{'top.px': divStyle}"> // Not sure if this is the correct approach as can't bind to divStyle in the directive
</div>
</ng-template>
</div>
我尝试使用 ngStyle,但不确定如何访问 divStyle 值,因为它是使用 viewContainerRef.createEmbeddedView 创建的。
我认为更好的选择是使用 style.bottom 从 ts 文件添加样式,但我不知道如何添加。我需要计算 tooltipDataArray.length,然后将 10px 左右添加到重新定位 viewContainerRef 的变量中。我不确定最好的方法。
@Input() tooltipDataArray: string[];
@ContentChild("tooltipTemplate") private tooltipTemplateRef: TemplateRef<Object>;
@HostListener("mouseenter") onMouseEnter(): void {
console.log(this.tooltipDataArray);
const view = this.viewContainerRef.createEmbeddedView(
this.tooltipTemplateRef
);
this.tooltipDataArray.forEach(el => {
const child = document.createElement("div");
child.innerText = el;
this.renderer.appendChild(view.rootNodes[1], child);
});
// Somthing like this.viewContainerRef.styles.bottom = 10 x this.tooltipDataArray.length + 'px'
console.log(view.rootNodes)
view.rootNodes.forEach(node => {
this.renderer.appendChild(this.elementRef.nativeElement, node);
});
}
@HostListener("mouseleave") onMouseLeave(): void {
if (this.viewContainerRef) {
this.viewContainerRef.clear();
}
【问题讨论】:
标签: angular dom angular-directive