【问题标题】:Structural directives, position tooltip结构指令,位置工具提示
【发布时间】: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


    【解决方案1】:

    我会通过在宿主元素中移动生成的工具提示来实现它,所以我将只使用 css 规则来定义位置:

    tooltip.directive.ts

    @Directive({ selector: '[tooltipDirective]' })
    export class TooltipDirective {
      private tooltipId: string;
    
      constructor(
          private renderer: Renderer2,
          private elementRef: ElementRef,
          private viewContainerRef: ViewContainerRef) { }
    
      @Input() parametroPlantilla: TemplateRef<any>;
    
      @ContentChild( "tooltipTemplate" ) private tooltipTemplateRef: TemplateRef<Object>;
    
      @HostListener('mouseenter')  onMouseEnter(): void {    
        const view = this.viewContainerRef.createEmbeddedView(this.tooltipTemplateRef);
        view.rootNodes.forEach(node => 
          this.renderer.appendChild(this.elementRef.nativeElement, node));
      }
    
      @HostListener('mouseleave') onMouseLeave(): void {        
        if (this.viewContainerRef) {
          this.viewContainerRef.clear();
        }  
      }  
    }
    

    html

    <div tooltipDirective>See tooltip!
      <ng-template #tooltipTemplate>      
          <div class="tooltip">   <================ add class
              This is my tooltip!
          </div>      
      </ng-template>  
    </div>
    

    css

    [tooltipDirective] {
      position: relative;
    }
    
    .tooltip {
      position: absolute;
      bottom: 100%;
      left: 0;
      padding: 10px;
      background: #fff;
      box-shadow: 0 2px 1px rgba(0,0,0,.6);
    }
    

    Stackblitz example

    【讨论】:

    • 非常感谢!有效!但我不确定这行代码的作用:“view.rootNodes.forEach(node => this.renderer.appendChild(this.elementRef.nativeElement, node)”你能解释一下吗?
    • 默认情况下,您的&lt;div class="tooltip" 标签放置在当前元素旁边。此代码将其插入&lt;div tooltopDirective
    • 有谁知道如何在显示工具提示时集成动画?
    • 你要应用哪个动画?
    • 一个像淡入淡出这样的动画,我不知道如何应用它
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2020-12-31
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    相关资源
    最近更新 更多