【问题标题】:Angular: Change host style if host is clickableAngular:如果主机可单击,则更改主机样式
【发布时间】:2022-01-20 23:06:03
【问题描述】:

如果组件上有(click)="..."属性,有没有办法改变宿主的样式?

类似这样的:

app.component.html

<my-example (click)="..."></my-component>

my-example.component.scss

:host {
 cursor: default;
}


/* If element has (click) attribute use this style */
:host-context([(click)]) {
 cursor:pointer;
}

【问题讨论】:

    标签: angular sass


    【解决方案1】:

    您可以使用 @HostBinding 装饰器来做到这一点。

    在您的 .ts 文件中,您需要将一些属性绑定到 style.cursor 属性并在需要时切换它。

    类似这样的:

      @HostBinding('style.cursor') styleCursor: 'default' | 'pointer' = 'default';
        
      showAlert(event) {
         this.styleCursor = this.styleCursor === 'default' ? 'pointer' : 'default';
         ....
      }
    

    【讨论】:

    • 您好,感谢您的回答。在上面的示例中,我想说明点击是从外部直接发生在主机上的。然后组件必须以某种方式知道 (click) 已添加到元素标记。这就是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多