如何在组件初始化时显示角度材质工具提示



我想在初始化/加载组件时显示角度材质工具提示。

我知道我可以添加一个 HTML 属性以在事件发生时显示它。我的总体目标是在组件加载时显示工具提示,然后在几秒钟后隐藏。

我尝试了以下方法:

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

YoukouleleY 几乎是正确的,你需要把它放到 ngAfterViewInit() 中并添加 setTimeout() 才能让它工作:

@ViewChild('tooltip') tooltip: MatTooltip;
constructor(private cd: ChangeDetectorRef) { }
ngAfterViewInit() {
   this.tooltip.show();
   this.cd.detectChanges();
   setTimeout(() => this.tooltip.hide(2000));
}

添加了 changeDetectorRef 的更新,以避免 ExpressionChangedAfterItHasBeenCheckedError。希望有帮助。

试试这个:

@ViewChild('tooltip') tooltip: MatToolTip;
ngOnInit() {
  this.tooltip.show();
  this.tooltip.hide(2000);
}

最新更新