角度材质-如何添加一个工具提示到一个禁用按钮

我已经注意到指令 matTooltip在禁用按钮上不起作用。我如何实现它?

例如:

<button mat-raised-button [disabled]="true" matTooltip="You cannot delete that">
<mat-icon>delete</mat-icon>
</button>

对于一个启用的按钮,它工作得非常完美:

<button mat-raised-button [disabled]="false" matTooltip="You cannot delete that">
<mat-icon>delete</mat-icon>
</button>
77043 次浏览

This doesn't work because it is triggered by mouseenter event which doesn't get fired by most browsers for disabled elements. A workaround is to add matTooltip to a parent element:

<div matTooltip="You cannot delete that" [matTooltipDisabled]="!isButtonDisabled()">
<button mat-raised-button [disabled]="isButtonDisabled()">
<mat-icon>delete</mat-icon>
</button>
</div>

The example above assumes that there is a method for determining if the button should be enabled or not. By using matTooltipDisabled the tooltip will be shown only if the button is disabled.

References:

I had a similar issue while displaying tooltip on a disabled icon button. The given solution was not practical for me, because adding an additional div on top of the button, messed up the layout of the button relative to the other buttons in the tool bar.

A simpler solution for me was to add the tooltip inside the icon inside the button. Something like this:

<button mat-raised-button [disabled]="true">
<mat-icon matTooltip="You cannot delete that">delete</mat-icon>
</button>

Since the icon is not disabled, it works.

Yes, the simplest solution is like above. But for my case I needed more flexibility.

   <button  [disabled]="form.invalid">
<span [matTooltip]="form.invalid ? 'some text' : ''">button text</span>
</button>

You can use title attribute it will display in necessary cases

<button mat-raised-button [disabled]="true" title = "Some text">
<mat-icon>delete</mat-icon>
</button>

you can do property binding with ternary operators

I find a solution!

Put the tooltip into the button content like this:

<button type="submit" [disabled]="disableEdit()" class="btn btn-primary btn-sm"
[routerLink]="['/entity', entity.id, 'edit']">
<div matTooltip="\{\{ 'entity.placeholders.cantEdit' | translate }}"
[matTooltipDisabled]="disableEdit()">
<fa-icon [icon]="'pencil-alt'"></fa-icon>
<span class="d-none d-md-inline">\{\{ 'entity.action.edit' | translate }}</span>
</div>
</button>

Try this:

<div [matTooltip]="isDisabled ? 'You cannot delete that' : ''">
<button mat-raised-button [disabled]="isDisabled">
<mat-icon>delete</mat-icon>
</button>
<div>

Adding tooltip inside mat-icon in a button as suggested by others will only work when you hover the icon not the button. Instead of that you can just wrap your button around another div without any css classes, just tooltip.

Additionally you can also add matTooltipDisabled property to make sure your tooltip is never disabled.

<div matTooltip="You cannot delete that" [matTooltipDisabled]="false">
<button mat-raised-button [disabled]="true">
<mat-icon>delete</mat-icon>
</button>
</div>

I know this is ugly but you also do this way, Use multiple buttons with ngIf

<!-- No click action -->
<button *ngIf="disable" mat-raised-button matTooltip="You cannot delete that">
<mat-icon>delete</mat-icon>
</button>
<button *ngIf="!disable" mat-raised-button (click)="delete()">
<mat-icon>delete</mat-icon>
</button>

Just add style 'pointer-events: all' to your button like this

<button mat-raised-button style="pointer-events: all" [disabled]="true" matTooltip="You cannot delete that">
<mat-icon>delete</mat-icon>
</button>

Not only matTooltip, even title attribute doesn't work on disabled button.

Just add the tooltip on its containing div, not on the button itself.

<div title="Some tooltip message">
<button type="button" [disabled]="!enable()">My Button</button>
</div>