[英]Use MatTooltip with host Angular component
I'm working with Angular 15.1 & Angular Material 15.1 and I need to use MatTooltip directive with the host component.我正在使用 Angular 15.1 & Angular Material 15.1,我需要对主机组件使用 MatTooltip 指令。 I know I can create a wrapper and use tooltip attribute - but I need a solution with the host component.
我知道我可以创建一个包装器并使用工具提示属性 - 但我需要一个包含主机组件的解决方案。
I tried to use MatTooltip like that:我试图像那样使用 MatTooltip:
host: {
'[attr.matTooltip]': 'some text',
}
and also with ViewContainerRef, createComponent method and @HostListener, but none of the options did work out以及 ViewContainerRef、createComponent 方法和 @HostListener,但没有一个选项有效
in Angular v15 you can use Directive composition API在 Angular v15 中,您可以使用指令组合 API
@Component({
selector: 'test',
template: 'test.html',
hostDirectives: [{
directive: MatTooltip,
inputs: [...],
outputs: [...],
}],
})
export class TestComponent { }
I've found an answer on my own, maybe someone will need this too.我自己找到了答案,也许有人也需要这个。 I will share code pieces below.
我将在下面分享代码片段。
@Component({
selector: 'test-component',
template: `
<div>test</div>
`,
providers: [MatTooltip],
styleUrls: ['./test.component.scss'],
})
...
constructor(
private readonly _tooltip: MatTooltip,
) {}
...
@HostListener('mouseenter') onMouseEnter(): void {
this._tooltip.message = 'your message';
this._tooltip.show();
}
@HostListener('mouseleave') onMouseLeave(): void {
this._tooltip.hide();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.