2

I want to show an Angular Material tooltip when its component 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 component loads, then hide after a few seconds.

I've tried the following:

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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That did solve my problem, however it also throws the error: Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'zoom: undefined'. Current value: 'zoom: null'. It seems like the view has been created after its parent and its children have been dirty checked. Has it been created in a change detection hook ?
@possum_pendulum I've updated answer, please check it.
0

Try this:

@ViewChild('tooltip') tooltip: MatToolTip;

ngOnInit() {
  this.tooltip.show();
  this.tooltip.hide(2000);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.