【问题标题】:Show Tooltip for Angular (ng2-tooltip-directive) only when the value is not undefined仅当值未定义时才显示 Angular 的工具提示(ng2-tooltip-directive)
【发布时间】:2020-01-30 12:58:18
【问题描述】:

我正在使用Tooltip for Angular (ng2-tooltip-directive) 在我的应用程序中显示工具提示。

以下面的代码为例,如何在每次title未定义时隐藏工具提示?

//HTML
<div tooltip="{{title}}"></div>

//TS
export class Component {
    title: string = undefined
}

注意:在这种情况下,工具提示显示时没有任何文字。

此外,在here 你有一个完整的可行的例子

【问题讨论】:

    标签: javascript html css angular ng2-tooltip-directive


    【解决方案1】:
    <span [tooltip]="errorMessage" [display]="showTooltip" content-type="template" placement="right" show-delay="100" hide-delay="500">
    

    ng2 工具提示有一个显示属性。你可以用那个。这里我为属性分配了一个布尔变量showTooltip。

    【讨论】:

      【解决方案2】:

      使用 *ngIf

      <ng-container *ngIf="title">
          <div tooltip="{{title}}"></div>
      <ng-container>
      <ng-container *ngIf="title == null">
           <div></div>
      <ng-container>
      

      【讨论】:

        【解决方案3】:

        我多次尝试使用ng2-tooltip-directive 中的条件隐藏工具提示。他们在 Pro 版本中提供元素选择和其他功能。所以它可以在专业版中完成。

        对于 Angular 材料设计,还有其他不错的工具提示库可用。

        注意:如果您正在迭代项目,则不建议使用*ngIf title and tooltip="",因为我们正在复制所有代码。

        如果您在项目中使用 bootstrap,则可以改用 ngbTooltip:

        https://ng-bootstrap.github.io/#/components/tooltip/examples

        npm module

        @ng-bootstrap/ng-bootstrap
        

        app.module.ts

        import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
        
        imports: [
          ....
          NgbModule,
          ....
        ]
        

        .html

        <ng-template #TooltipContent>
         <div>This file belongs to <b>
          {{file.owner.name}}</b>
         </div>
        </ng-template> 
        
        <div *ngFor="let file of files">
          .....
          <div [ngbTooltip]="file.owner.name ? TooltipContent : ''">
            .....
            .....
          </div>
          .....
          .....
        </div>
        

        在上面的示例中,工具提示仅在文件具有所有者时显示。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-04-03
          • 1970-01-01
          • 2020-02-12
          • 1970-01-01
          • 2013-10-08
          • 1970-01-01
          • 2011-10-19
          相关资源
          最近更新 更多