Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material 2 Md-ToolTip with show conditionally

Tags:

I am trying to use Angular Material 2's MdToolTip. The syntax looks like

<span mdTooltip="Tooltip!">I have a tooltip</span> 

However, I want to implement this function on my anchor tag. I want to show the tooltip when I hover over the ahchor tag when the class="not-active" is in action. How could I achieve this?

<a [ngClass]="{'not-active': !isCurrentUserExist}" [routerLink]="['/create-timesheet']">Link1</a>   /*disabled side menu links*/ .not-active {    pointer-events: none;    cursor: default; } 
like image 503
ErnieKev Avatar asked Feb 16 '17 02:02

ErnieKev


People also ask

How do I display tooltip in angular 8?

Showing and hiding By default, the tooltip will be immediately shown when the user's mouse hovers over the tooltip's trigger element and immediately hides when the user's mouse leaves. On mobile, the tooltip is displayed when the user longpresses the element and hides after a delay of 1500ms.

What is MatTooltip?

MatTooltip. Directive that attaches a material design tooltip to the host element. Animates the showing and hiding of a tooltip provided position (defaults to below the element). https://material.io/design/components/tooltips.html.


2 Answers

I want to show the tooltip when I hover over the ahchor tag when the class="not-active" is in action.

So, basically, the .not-active class is enabled when the variable isCurrentUserExist evaluates to false, right? (That's what your code is showing).

Then, you can achieve it simply putting a condition in [matTooltip] @Input:

<span [matTooltip]="!isCurrentUserExist ? 'Tooltip!' : ''">   I have a tooltip </span> 

Edit 1

For a more elegant solution, we can use matTooltipDisabled @Input (which one was implemented in PR#3578 and released in @angular/[email protected] cesium-cephalopod):

<span matTooltip="Tooltip!" [matTooltipDisabled]="isCurrentUserExist">   I have a tooltip </span> 
like image 163
developer033 Avatar answered Oct 12 '22 00:10

developer033


The Material Angular Tooltip has a parameter called matTooltipDisabled (type boolean) made for that. It can be bound to the same element as the matTooltip is being bound.

<span matTooltip="Tooltip!" [matTooltipDisabled]="hideTooltip == true">I have a tooltip</span> 

Don't forget to declare the variable and set a value ;)

let hideTooltip:boolean = false; 

So, using your own code, the better solution for you would be:

<a matTooltip="Tooltip!" [matTooltipDisabled]="!isCurrentUserExist" [ngClass]="{'not-active': !isCurrentUserExist}" [routerLink]="['/create-timesheet']">Link1</a>  /*disabled side menu links*/ .not-active {    pointer-events: none;    cursor: default; } 

Example for you: https://stackblitz.com/edit/angular-conditional-tooltip

Docs: https://material.angular.io/components/tooltip/overview#disabling-the-tooltip-from-showing

like image 24
ErickXavier Avatar answered Oct 11 '22 23:10

ErickXavier