javascript - How to show Angular Material Tooltip on component initialization - Stack Overflow

admin2025-04-19  0

I want to show an Angular Material tooltip when its ponent is initialized/loaded.

I know I can add an HTML attribute to show it when an event happens. My overall goal is to have the tooltip showing when the ponent loads, then hide after a few seconds.

I've tried the following:

<div (load)="tooltip.show()"
     #tooltip="matTooltip"
     matTooltip="blah blah">
</div>

I want to show an Angular Material tooltip when its ponent is initialized/loaded.

I know I can add an HTML attribute to show it when an event happens. My overall goal is to have the tooltip showing when the ponent loads, then hide after a few seconds.

I've tried the following:

<div (load)="tooltip.show()"
     #tooltip="matTooltip"
     matTooltip="blah blah">
</div>
Share Improve this question edited Jan 28, 2019 at 21:23 possum_pendulum asked Jan 28, 2019 at 21:13 possum_pendulumpossum_pendulum 1791 gold badge4 silver badges21 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

YoukouleleY is almost correct, you need to put it into ngAfterViewInit() and add setTimeout() to make it work:

@ViewChild('tooltip') tooltip: MatTooltip;

constructor(private cd: ChangeDetectorRef) { }

ngAfterViewInit() {
   this.tooltip.show();
   this.cd.detectChanges();
   setTimeout(() => this.tooltip.hide(2000));
}

Added update with changeDetectorRef to avoid ExpressionChangedAfterItHasBeenCheckedError. Hope that helps.

Try this:

@ViewChild('tooltip') tooltip: MatToolTip;

ngOnInit() {
  this.tooltip.show();
  this.tooltip.hide(2000);
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745014548a280010.html

最新回复(0)