首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何编程触发工具提示以角2/4显示?

如何编程触发工具提示以角2/4显示?
EN

Stack Overflow用户
提问于 2017-07-20 20:25:46
回答 1查看 3.9K关注 0票数 2

我有一个工具提示“你好”,我想显示当我(点击)在一个按钮。看过这个:https://swimlane.github.io/ngx-ui/#tooltip和其他工具提示库,但它们似乎都需要使用dom,并且不以编程方式打开和关闭。

在角1中,您可以这样做:http://plnkr.co/edit/QeQqqEJAu1dCuDtSvomD?p=preview

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<!-- 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工具提示无法做到这一点),我正在使用引导和上面引用的库作为我的工具提示。如果有另一个图书馆我可以这样做,那就太好了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-20 21:57:16

您必须创建一个工具提示指令,根据您的要求,下面是相同的代码,我支持鼠标通过和焦点。

指令:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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();
        }
    }
}
}

组件中的

在这里,dataContext是一个包含工具提示细节的对象,比如描述和其他,您可以根据需要进行配置。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<div Tooltip [dataContext]="dataContext"></div>

输出:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45228955

复制
相关文章
PowerBI 工具提示 在图上显示图
很多人会好奇的是,这个页面如何随着其他的图而变呢?其道理在于它会受到其他图的筛选。
BI佐罗
2020/08/20
2.3K0
[视频号]不用编程也能动态显示/隐藏提示
有时候,我们想在工作表中放置一些操作提示,在用户需要时显示,不需要时可以隐藏,但又不想使用VBA,那该怎么办呢?
fanjy
2021/10/15
3.4K0
[视频号]不用编程也能动态显示/隐藏提示
Mac上如何设置使用触发角快速启动屏幕保护程序
如果您使用屏幕保护程序,它会在您的 Mac 不活跃一段时间后自动启动。您可以设定快捷方式,以便在将指针移到屏幕边角时启动屏幕保护程序。
MAC先森
2019/09/26
2.6K0
4流量获取工具,以帮助您发展业务
本文介绍了一些可以帮助您发展业务的流量获取工具,这些工具不需要大量的编码知识。其中包括Twitter、Nimble、Colibri和MixRank。
Techeek
2018/01/08
8290
以JSONP触发的知识点
同源指:协议+域名+端口,三者统一 限制行为: 1)Cookie、LocalStorage、IndexDB无法读取 2)DOM和JS对象无法获得 3)AJAX请求无法发送
杨肆月
2019/09/29
4050
以JSONP触发的知识点
VBA编程练习08:删除工作表而不显示提示信息
当我们删除工作簿中的工作表时,Excel会自动弹出“MicrosoftExcel将永久删除此工作表。是否继续?”的信息提示框,要求你选择“删除”还是“取消”,如下图1所示。
fanjy
2021/06/01
4.1K0
VBA编程练习08:删除工作表而不显示提示信息
提示以只读方式打开文件
如果不希望内容审阅者意外修改你的文档,可在发送文档供审阅前将其设为只读。 另一种方法是限制格式和编辑。
MIKE笔记
2023/03/22
1.8K0
提示以只读方式打开文件
如何取消-"插入耳机自动显示提示框"
首先我们打开控制面板->1,你可以直接搜索控制面板打开 2,你可以右击我的电脑->点击属性->左上角打开控制面板
程序员小藕
2020/07/28
1.1K0
如何取消-"插入耳机自动显示提示框"
读者提问,如何让 tooltip 提示框内显示饼图
前些天有读者问,ECharts 的提示框(tooltip)内,能不能添加一个饼图?
ZXand618
2022/04/10
1.7K0
读者提问,如何让 tooltip 提示框内显示饼图
FFMPEG读取v4l2并显示!
最近问v4l2的人挺多的,等忙完这段时间,后面有空研究一下。今天给大家分享一些应用demo;
用户6280468
2022/11/28
1.9K0
FFMPEG读取v4l2并显示!
IntelliJ IDEA 如何显示工具栏
https://www.ossez.com/t/intellij-idea/13867
HoneyMoose
2022/01/28
2.8K0
IntelliJ IDEA 如何显示工具栏
Linux登录后显示提示信息
Linux可以设置登录前后的欢迎信息,虽然没啥技术含量,但却是非常实用的一个小技巧。 实现登录消息的功能,可以修改3个文件。
知识浅谈
2020/03/25
4.9K0
(译)SDL编程入门(2)在屏幕上显示图像
注意:从现在开始,教程将只涉及源代码的关键部分。如果想看完整的程序,你必须下载完整的源码。
arcticfox
2020/09/24
2.7K0
2个list取交集_角的集合如何取交集
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
全栈程序员站长
2022/11/08
1.1K0
纯CSS实现小三角提示信息
实现后的效果如下: 不带边框的 带边框的 在此提供两种实现方式: 1、不带边框的 css: *{margin:0;padding:0;} body{ backg
牛老师讲GIS
2018/10/23
1.3K0
纯CSS实现小三角提示信息
Toast提示工具类
APP开发中我们常用的与用户交互的消息就是Toast 但是Android原生的Toast 比较繁琐, 我们来比较一下
晨曦_LLW
2020/09/25
6470
ChatGPT-4提示工程
提示工程是一门精细的艺术,其目的是设计问题或陈述,也称为“提示”,以从人工智能(AI)模型中提取特定的回答。
yeedomliu
2023/09/03
2620
ChatGPT-4提示工程
点击加载更多

相似问题

以编程方式显示工具提示

33

以编程方式显示SVG工具提示

10

以编程方式显示工具提示图

10

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

322

使用jqueryui以编程方式显示工具提示

21
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文