千家信息网

Angular中怎么以Tooltip自定义指令

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本文小编为大家详细介绍"Angular中怎么以Tooltip自定义指令",内容详细,步骤清晰,细节处理妥当,希望这篇"Angular中怎么以Tooltip自定义指令"文章能帮助大家解决疑惑,下面跟着小
千家信息网最后更新 2026年11月01日Angular中怎么以Tooltip自定义指令

本文小编为大家详细介绍"Angular中怎么以Tooltip自定义指令",内容详细,步骤清晰,细节处理妥当,希望这篇"Angular中怎么以Tooltip自定义指令"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。


效果图如下:

目录结构

在上一篇文章的实现的代码项目基础上,执行命令行:

# 进入 directives 文件夹$ cd directives# 创建 tooltip 文件夹$ mkdir tooltip# 进入 tooltip 文件夹$ cd tooltip# 创建 tooltip 组件$ ng generate component tooltip# 创建 tooltip 指令$ ng generate directive tooltip

执行完上面的命令行之后,你会得到 app/directive/tooltip 的文件目录结构如下:

tooltip├── tooltip                                           // tooltip 组件│    ├── user-list.component.html                     // 页面骨架│    ├── user-list.component.scss                     // 页面独有样式│    ├── user-list.component.spec.ts                  // 测试文件│    └── user-list.component.ts                       // javascript 文件├── tooltip.directive.spec.ts                         // 测试文件└── tooltip.directive.ts                              // 指令文件

嗯,这里我将组件放在 tooltip 的同级,主要是方便管理。当然,这个因人而异,你可以放在公共组件 components 文件夹内。

编写 tooltip 组件

在 html 文件中,有:

{{data.content}}

在样式文件 .scss 中,有:

$black: #000000;$white: #ffffff;$caret-size: 6px;$tooltip-bg: transparentize($black, 0.25); // transparentize 是 sass 的语法$grid-gutter-width: 30px;$body-bg-color: $white;$app-anim-time: 200ms;$app-anim-curve: ease-out;$std-border-radius: 5px;$zindex-max: 100;// :host 伪类选择器,给组件元素本身设置样式:host {  position: fixed;  padding: $grid-gutter-width/3 $grid-gutter-width/2;  background-color: $tooltip-bg;  color: $body-bg-color;  opacity: 0;  transition: all $app-anim-time $app-anim-curve;  text-align: center;  border-radius: $std-border-radius;  z-index: $zindex-max;}.caret { // 脱字符  width: 0;  height: 0;  border-left: 6px solid transparent;  border-right: 6px solid transparent;  border-bottom: 6px solid $tooltip-bg;  position: absolute;  top: -$caret-size;  left: 50%;  margin-left: -$caret-size/2;  border-bottom-color: $tooltip-bg;}

嗯~,css 是一个神奇的东西,之后会安排一篇文章来讲解下 sass 相关的内容...

然后,在 javascript 文件 tooltip.component.ts 内容如下:

import {   Component,   ElementRef, // 元素指向  HostBinding,   OnDestroy,   OnInit } from '@angular/core';@Component({  selector: 'app-tooltip', // 标识符,表明我这个组件叫做啥,这里是 app-tooltip  templateUrl: './tooltip.component.html', // 本组件的骨架  styleUrls: ['./tooltip.component.scss'] // 本组件的私有样式})export class TooltipComponent implements OnInit {  public data: any; // 在 directive 上赋值  private displayTimeOut:any;  // 组件本身 host 绑定相关的装饰器  @HostBinding('style.top')  hostStyleTop!: string;  @HostBinding('style.left') hostStyleLeft!: string;  @HostBinding('style.opacity') hostStyleOpacity!: string;  constructor(    private elementRef: ElementRef  ) { }  ngOnInit(): void {    this.hostStyleTop = this.data.elementPosition.bottom + 'px';    if(this.displayTimeOut) {      clearTimeout(this.displayTimeOut)    }    this.displayTimeOut = setTimeout((_: any) => {      // 这里计算 tooltip 距离左侧的距离,这里计算公式是:tooltip.left + 目标元素的.width - (tooltip.width/2)      this.hostStyleLeft = this.data.elementPosition.left + this.data.element.clientWidth / 2 - this.elementRef.nativeElement.clientWidth / 2 + 'px'      this.hostStyleOpacity = '1';      this.hostStyleTop = this.data.elementPosition.bottom + 10 + 'px'    }, 500)  }      // 组件销毁  ngOnDestroy() {    // 组件销毁后,清除定时器,防止内存泄露    if(this.displayTimeOut) {      clearTimeout(this.displayTimeOut)    }  }}

编写 tooltip 指令

这是本文的重点,具体的说明,我在代码上标注出来~

相关的文件 tooltip.directive.ts 内容如下:

import {   ApplicationRef, // 全局性调用检测  ComponentFactoryResolver, // 创建组件对象  ComponentRef, // 组件实例的关联和指引,指向 ComponentFactory 创建的元素  Directive, ElementRef,   EmbeddedViewRef, // EmbeddedViewRef 继承于 ViewRef,用于表示模板元素中定义的 UI 元素。  HostListener, // DOM 事件监听  Injector, // 依赖注入  Input } from '@angular/core';import { TooltipComponent } from './tooltip/tooltip.component';@Directive({  selector: '[appTooltip]'})export class TooltipDirective {  @Input("appTooltip") appTooltip!: string;  private componentRef!: ComponentRef;  // 获取目标元素的相关位置,比如 left, right, top, bottom  get elementPosition() {    return this.elementRef.nativeElement.getBoundingClientRect();   }  constructor(    protected elementRef: ElementRef,    protected appRef: ApplicationRef,    protected componentFactoryResolver: ComponentFactoryResolver,    protected injector: Injector  ) { }  // 创建 tooltip  protected createTooltip() {    this.componentRef = this.componentFactoryResolver      .resolveComponentFactory(TooltipComponent) // 绑定 tooltip 组件      .create(this.injector);    this.componentRef.instance.data = { // 绑定 data 数据      content: this.appTooltip,      element: this.elementRef.nativeElement,      elementPosition: this.elementPosition    }    this.appRef.attachView(this.componentRef.hostView); // 添加视图    const domElem = (this.componentRef.hostView as EmbeddedViewRef).rootNodes[0] as HTMLElement;    document.body.appendChild(domElem);  }    // 删除 tooltip  protected destroyTooltip() {    if(this.componentRef) {      this.appRef.detachView(this.componentRef.hostView); // 移除视图      this.componentRef.destroy();    }  }    // 监听鼠标移入  @HostListener('mouseover')  OnEnter() {    this.createTooltip();  }      // 监听鼠标移出  @HostListener('mouseout')  OnOut() {    this.destroyTooltip();  }}

到这里,已经完成了 99% 的功能了,下面我们在页面上调用即可。

页面上调用

我们在 user-list.component.html 上添加下面的内容:

Jimmy

TooltipDirective 这个指令我们已经在 app.module.ts 上进行声明,我们直接调用即可。目前的效果如下:

我们实现的 tooltip 是底部居中展示,也就是我们平常使用框架,比如 angular ant design 中 tooltip 的 bottom 属性。对于其他属性,读者感兴趣的话,可以进行扩展。

读到这里,这篇"Angular中怎么以Tooltip自定义指令"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。

组件 文件 指令 元素 内容 文件夹 样式 页面 文章 篇文章 监听 代码 命令 属性 指向 效果 目录 目标 结构 视图 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 百单网互联网科技有限公司 广州为想互联网科技有限公司 互联网电商科技 疫情对互联网科技公司影响 河南真二互联网科技有限公司 深圳科技互联网 北京互联网科技 安徽鑫晟辉互联网科技 郑州华宇互联网科技 彤创互联网科技有限公司 世界互联网科技公司市值排名 玉溪互联网科技怎么选 广州新华互联网科技学校电竞方面 易企秀互联网科技是做什么的 江苏互联网智能科技产品出厂价 国产互联网科技公司 保山互联网科技靠谱吗 互联网科技的前景 高科技和互联网的现代国家 山东亿川互联网科技有限责任公司 互联网高科技 左右互联网络科技 开一家科技互联网公司需要多少钱 本届互联网博览会上的黑科技 互联网行业科技股 沐思想互联网科技有限公司 广州千川互联网科技有限公司 普陀区互联网教育科技管理办法 保山互联网科技靠谱吗 北京天晋互联网科技有限公司
0