如何以编程方式触发工具提示以在Angular 2/4中显示?

罗兰多

我有一个工具提示“你好”,当我(单击)按钮时要显示。正在查看以下内容:https : //swimlane.github.io/ngx-ui/#tooltip和其他工具提示库,但是所有这些似乎都需要使用dom,并且不会以编程方式打开和关闭。

在角度1中,您可以执行以下操作:http : //plnkr.co/edit/QeQqqEJAu1dCuDtSvomD?p=preview

<!-- Popover can be controller by the following checkbox -->
<label>
  <input type="checkbox" ng-model="isOpen">
  show popover?
</label>

<br>

<!-- isOpen is a $scope variable -->
<span popover="Hello!" 
      popover-toggle="isOpen"
      popover-placement="bottom">
  Popover below
</span>

有没有可以使用Angular2做到这一点的库/方式?(如果ngx-ui工具提示无法执行此操作)我正在使用引导程序和上面为我的工具提示引用的库。如果还有其他某种或某种方式可以执行此操作的库,那就太好了。

Sandip-全栈开发人员

您必须根据自己的要求创建一个工具提示指令,以下是相同的代码,我支持将鼠标悬停在焦点上。

指示:

import { Directive, ElementRef, Input, HostListener, Renderer } from '@angular/core';

@Directive(
    {
        selector: '[Tooltip]',
    }
)

export class TooltipDirective {

constructor(public el: ElementRef, public renderer: Renderer) { }
tooltipTitle: any = '';
tooltipText: any = '';
tooltipImage: any = '';
isFormFieldModel: boolean = false;
@Input() dataContext: any;
@Input() IsButtonPanel: boolean = false;

private mouseTop: number = 0;
private mouseLeft: number = 0;
tooltipTop: number = 0;
tooltipLeft: number = 0;

@HostListener('click') onclick() {
    this.hover(false);
}

@HostListener('mouseenter', ['$event']) onMouseEnter(event: MouseEvent) {
    this.SetTooltipDetails(event);
}
@HostListener('mouseleave') onMouseLeave() {
    this.hover(false);
}

@HostListener('focusin') onFocus() {
    this.SetTooltipDetails(null);
}

@HostListener('focusout') onFocusout(target) {
    this.hover(false);
}

SetTooltipDetails(event: MouseEvent)
{
    this.hover(false);
    if (this.mainDiv != null) {
        this.mainDiv.remove();
        this.ImgElement.remove();
    }


    if (event != null) {
        this.mouseLeft = event.clientX;
        this.mouseTop = event.clientY;
    }
    else
    {
        this.mouseLeft = this.el.nativeElement.getBoundingClientRect().left;
        this.mouseTop = this.el.nativeElement.getBoundingClientRect().top + 20;
    }         

    if (this.dataContext != null) {

        this.tooltipText = this.dataContext.Description;           

        if (this.isFormFieldModel) {
            if (!this.dataContext.IsShowToolTip) {
                return;
            }
            if (this.dataContext.UseContextHeader == true && this.dataContext.ContextHeaderValue != null) {
                this.tooltipTitle = this.dataContext.ContextHeaderValue;
            }
            else {
                this.tooltipTitle = this.dataContext.PrettyName;
            }
        }
        else {                
            this.tooltipTitle = this.dataContext.Header;
            this.tooltipImage = this.dataContext.Icon;
        }

        if (this.tooltipTitle == '' || this.tooltipTitle == null || this.tooltipTitle == 'null') {
            this.tooltipTitle = "Header";
        }

        if (this.tooltipText == null || this.tooltipText == 'null') {
            this.tooltipText = "";
        }

        if (this.tooltipImage==null || this.tooltipImage == '' || this.tooltipImage == 'null') {
            this.tooltipImage = "info.png";
        }

        this.hover(true);
    }
}    

mainDiv: any; ImgElement: any; InputElement: any; divElement: any; divElement1: any; divElement2: any;
hover(onMouseHover: boolean) {

    if (onMouseHover && !this.IsButtonPanel) {
        //Dynamically Create Img Element   

        var elementTooltipItem = this.el.nativeElement.getElementsByClassName("tooltipMain")[0];
        if (elementTooltipItem != null) {
            elementTooltipItem.outerHTML = '';
        }            
        else
        {
            let tooltipItem = this.el.nativeElement.nextElementSibling;
            if (tooltipItem != null && tooltipItem.className.indexOf("tooltipMain") >= 0)
            {
                tooltipItem.outerHTML = '';
            }
        }

        this.ImgElement = this.renderer.createElement(this.el.nativeElement, "img");

        this.renderer.setElementAttribute(this.ImgElement, "src", "images/" + this.tooltipImage);

        this.renderer.setElementStyle(this.ImgElement, "width", "40px");
        this.renderer.setElementStyle(this.ImgElement, "height", "40px");
        this.renderer.setElementStyle(this.ImgElement, "margin-right", "2px");
        this.renderer.setElementStyle(this.ImgElement, "float", "left");
        this.renderer.setElementStyle(this.ImgElement, "border", "1px solid #CCC");
        this.renderer.setElementStyle(this.ImgElement, "border-radius", "5px");
        this.renderer.setElementStyle(this.ImgElement, "padding", "5px");
        this.renderer.setElementStyle(this.ImgElement, "backgroundColor", "#f5f5f5");
        this.renderer.setElementStyle(this.ImgElement, "display", "block");

        //tooltip text outer div

        this.divElement = this.renderer.createElement(this.el.nativeElement, "div");

        this.renderer.setElementStyle(this.divElement, "border", "1px solid #CCC");
        this.renderer.setElementStyle(this.divElement, "margin-left", "38px !important");
        this.renderer.setElementStyle(this.divElement, "color", "black");
        this.renderer.setElementStyle(this.divElement, "border-radius", "5px");
        this.renderer.setElementStyle(this.divElement, "padding", "5px");
        this.renderer.setElementStyle(this.divElement, "float", "left");
        this.renderer.setElementStyle(this.divElement, "backgroundColor", "#f5f5f5");
        this.renderer.setElementStyle(this.divElement, "text-align", "left !important");

        //tooltip text header div

        this.divElement1 = this.renderer.createElement(this.el.nativeElement, "div");

        this.renderer.setElementClass(this.divElement1, "header", true);
        this.renderer.createText(this.divElement1, this.tooltipTitle);


        //tooltip text description div

        this.divElement2 = this.renderer.createElement(this.el.nativeElement, "div");

        this.renderer.setElementClass(this.divElement2, "description", true);
        this.renderer.createText(this.divElement2, this.tooltipText);


        this.mainDiv = this.renderer.createElement(this.el.nativeElement, "div");

        this.renderer.setElementProperty(this.mainDiv, "disabled", true);
        this.renderer.setElementClass(this.mainDiv, "tooltipMain", true);

        this.mainDiv.appendChild(this.ImgElement);
        this.divElement.appendChild(this.divElement1);
        this.divElement.appendChild(this.divElement2);
        this.mainDiv.appendChild(this.divElement);


        let tooltipWidth = this.mainDiv.clientWidth + 10;
        let tooltipHeight = this.mainDiv.clientHeight + 10;

        let windowWidth = window.innerWidth;
        let windowHeight = window.innerHeight;

        if ((windowWidth - this.mouseLeft) < tooltipWidth) {
            //this.tooltipLeft = windowWidth - (tooltipWidth);
            this.renderer.setElementStyle(this.mainDiv, "right", "0px");
        } else {
            //this.tooltipLeft = this.mouseLeft;
            this.renderer.setElementStyle(this.mainDiv, "left", this.mouseLeft + "px");
        }

        if ((windowHeight - this.mouseTop) < tooltipHeight) {
            this.tooltipTop = this.mouseTop - 20;
            this.renderer.setElementStyle(this.mainDiv, "bottom", "0px");
        } else {
            this.renderer.setElementStyle(this.mainDiv, "top", this.mouseTop + 5 + "px");
        }


        //this.renderer.setElementStyle(this.mainDiv, "left", this.tooltipLeft + "px");
        //this.renderer.setElementStyle(this.mainDiv, "top", this.tooltipTop + "px");  
    }
    else {
        if (this.mainDiv != null) {
            this.mainDiv.remove();
            this.ImgElement.remove();
        }
    }
}
}

在组件HTML中:

这里的dataContext是一个对象,其中包含工具提示详细信息,例如description和其他,您可以根据需要进行配置

<div Tooltip [dataContext]="dataContext"></div>

输出:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当工具提示合并多个系列的数据时,如何以编程方式显示我的图表工具提示?(同步图表)

Swift-如何以编程方式显示“粘贴”工具提示菜单?

以编程方式显示/隐藏Android工具提示

如何以編程方式在 .NET 框架表單的工具提示中添加換行符

如何以编程方式消除SearchView中的SearchView提示图标占用的空间

如何以编程方式标记可在云工具包中查询的记录类型

如何以编程方式显示路线?

如何以编程方式显示/隐藏BottomAppBar?

如何以编程方式显示ViewController?

如何以编程方式在面板中显示内容?

如何以编程方式在CoordinatorLayout中显示折叠的元素?

如何以编程方式打开(显示在屏幕中)NavigationView

如何以编程方式突出显示QTreeView中的选择?

如何以编程方式突出显示DateTimePicker中的特定部分?

在Nextjs中,如何以编程方式触发服务器端渲染?

我如何以编程方式设置UITabBarController的选定选项卡,同时还触发UITabBarControllerDelegate中的shouldSelectViewController

如何在可可 (OS X) 中以编程方式自定义工具提示?

在Material-UI中以编程方式打开工具提示

如何以编程方式打开传单标记的工具提示?

如何以编程方式找到我在Angular中拥有的资产文件的路径?

如何以编程方式检查元素并在angular中添加类?

如何以编程方式检查表单控件在Angular中是否有效?

如何以编程方式将参数传递给angular2 +中的辅助路径?

如何以编程方式重置或取消选中 Angular 中的单选按钮?

您如何以编程方式在有角度的应用程序中的选择字段上触发点击事件?

如何在 Highcharts 上的 xAxis 中显示 24 小时

如何以编程方式显示/隐藏导航抽屉

OS X故事板:如何以编程方式显示窗口?

如何以编程方式多次显示相同的 ImageView?