设置自定义属性时主机的角度变化样式

Angular change style of host when custom attribute is set

提问人:André Schmidt 提问时间:11/16/2023 更新时间:11/16/2023 访问量:23

问:

我想根据主机标签上的自定义属性(只是属性,没有键值)更改自定义 Angular 组件的样式。但不知何故,':host[...]' 选择器没有被使用/没有改变样式。

有谁知道为什么这不起作用?以及如何让它发挥作用?

我的自定义组件如下所示

import { Component, ViewEncapsulation } from '@angular/core';

@Component({
  selector: 'my-component',
  template: `
    <div>
      <p>Lorem ipsum dolor sit amet</p>
    </div>
  `,
  styleUrls: ['./my-component.component.scss'],
  encapsulation: ViewEncapsulation.ShadowDom
})
export class MyComponentComponent {

}

css/scss-文件:

:host[custom] div {
  background: lightblue;
  border: 1px solid black;
  font-size: 20px;
  height: 150px;
  padding: 25px;
  width: 150px;
}

:host div {
  background: white;
  border: 1px solid black;
  font-size: 20px;
  height: 150px;
  padding: 25px;
  width: 150px;
}

app.component.html

<div>
  <div style='margin: 25px'>
    <my-component></my-component>
  </div>

  <div style='margin: 25px'>
    <my-component custom></my-component>
  </div>

</div>

结果:

Result

css angular sass 自定义组件

评论

0赞 oksd 11/16/2023
试试这个:<my-component [attr.custom]="true"></my-component>
1赞 Danny '365CSI' Engelman 11/16/2023
和?:host([custom])
0赞 André Schmidt 11/16/2023
:host([custom]) 修复了它,谢谢

答: 暂无答案