[英]How to show Angular Material Tooltip on component initialization
I want to show an Angular Material tooltip when its component is initialized/loaded. 我想在初始化/加载其组件时显示Angular Material工具提示。
I know I can add an HTML attribute to show it when an event happens. 我知道我可以添加HTML属性以在事件发生时显示它。 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>
YoukouleleY is almost correct, you need to put it into ngAfterViewInit() and add setTimeout() to make it work: YoukouleleY几乎是正确的,您需要将其放入ngAfterViewInit()并添加setTimeout()使其起作用:
@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. 添加了带有changeDetectorRef的更新,以避免ExpressionChangedAfterItHaHasBeenCheckedError。 Hope that helps. 希望能有所帮助。
Try this: 尝试这个:
@ViewChild('tooltip') tooltip: MatToolTip;
ngOnInit() {
this.tooltip.show();
this.tooltip.hide(2000);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.