如何在 angular 2 中动态更改 :host 中的 CSS?

How to dynamically change CSS in :host in angular 2?

如何动态更改 CSS 组件主机的属性?

我有一个组件 CSS 我给了它一个stlye:

:host {
  overflow-x: hidden
}

在从子组件单击按钮时,我需要将 overflow-y: hidden 添加到主机组件。

如何实现此行为?

Here is a working example.

使用以下主机绑定:

@HostBinding('style.overflow-y') overflowY = 'scroll';

这将给出以下组件:

@Component({
    selector: 'my-app',
    template: `
          <div>
            <button (click)="addStyle()">Add Style</button>
            <h2>Hello</h2>
          </div>`, styles: [
        `
        :host {
          overflow-x: hidden;
          height: 50px;
          width: 200px;
          display: block;
        }
        `,
    ],
})
export class App {
    name: string;

    @HostBinding('style.overflow-y')
    overflowY = 'scroll';

    constructor() {
    }

    addStyle() {
        this.overflowY = 'hidden';
    }
}