简体   繁体   English

如何通过 Angular 自定义组件传递 class

[英]How to pass class through an Angular Custom component

So I have a tooltip custom component that I need to be able to pass a class through to the underlying html, but not sure how to do it.所以我有一个工具提示自定义组件,我需要能够将 class 传递到底层 html,但不知道该怎么做。 Currently the tooltip (custom angular component) html looks like this:目前工具提示(自定义 angular 组件)html 如下所示:

<div role="tooltip" aria-haspopup="true" class="tooltip tooltip-md tooltip-bottom-right">
    <clr-icon class="icon-shape is-solid" shape="info-circle" size="20"></clr-icon>
    <div class="tooltip-content">
        <ng-content></ng-content>
    </div>
</div>

Using this custom component is like:使用这个自定义组件就像:

<tooltip>This is the text displayed.</tooltip>

What I need to be able to do is pass in a position so that the tooltip isn't always at bottom-right.我需要做的是传入 position 以便工具提示不总是在右下角。 So I need to pass either a property or class or something so that in the custom component I can change the class of my div to "tooltip-top-right", "tooltip-bottom-left", etc... like so:所以我需要传递一个属性或 class 或其他东西,以便在自定义组件中我可以将我的 div 的 class 更改为“tooltip-top-right”、“tooltip-bottom-left”等......就像这样:

<tooltip class="tooltip-bottom-left">Tooltip text</tooltip>

or或者

<tooltip position="bottom-left">Tooltip text</tooltip>

And then inside the component's.ts or.html, assign the appropriate class to my div.然后在组件的.ts 或.html 中,将适当的class 分配给我的div。
Thanks in advance!提前致谢!

You could use mat-tooltip for this case.在这种情况下,您可以使用 mat-tooltip。 https://material.angular.io/components/tooltip/overview https://material.angular.io/components/tooltip/overview

It has built in matTooltipPosition directive so you wouldnt need to add any class to it.它内置了 matTooltipPosition 指令,因此您无需向其添加任何 class。

1) Import the appropriate module in app.module.ts 1) 在 app.module.ts 中导入相应的模块

import {MatTooltipModule} from '@angular/material/tooltip';

2) Use the mat-tooltip selector and the matTooltipPosition input property 2) 使用 mat-tooltip 选择器和 matTooltipPosition 输入属性

<button mat-raised-button
    matTooltip="Info about the action"
   [matTooltipPosition]='where you want it to be'
    aria-label="Button that displays a tooltip when focused or hovered over">
  Action
 </button>

more examples: https://material.angular.io/components/tooltip/examples更多示例: https://material.angular.io/components/tooltip/examples

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM