要在Angular Material的matTooltip中显示matError,你需要自定义matTooltip的模板,并在其中添加mat-error。以下是一个示例解决方案:
首先,确保你已经安装了Angular Material和相关依赖。
然后,创建一个新的组件(例如TooltipComponent):
tooltip.component.html:
{{ errorMessage }}
tooltip.component.ts:
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-tooltip',
templateUrl: './tooltip.component.html',
styleUrls: ['./tooltip.component.css']
})
export class TooltipComponent {
@Input() errorMessage: string;
}
接下来,在使用matTooltip的组件中,使用matTooltip来引用自定义的tooltip模板:
这里的tooltipTemplate是一个自定义模板,它使用了前面创建的TooltipComponent,并传递了errorMessage属性。
最后,在使用matTooltip的组件中,定义errorMessage属性,以便在TooltipComponent中显示错误消息:
export class YourComponent {
errorMessage = 'This is an error message';
}
现在,当鼠标悬停在按钮上时,将显示带有错误消息的matTooltip。
希望这可以帮助到你!