将有角度的宿主有条件地应用于组件标签

问题描述 投票:0回答:1

仅当情况为真时,我才尝试将一个类应用于组件标签。

如何将此主机转换为条件主机,以便在需要时应用所需的类?

@Component({
  selector: 'k-sidebar',
  host: {
   class: '{{isChanged}}'
 },
 templateUrl: './ng-k-side-bar.component.html',
 encapsulation: ViewEncapsulation.None

})
angular angular-components
1个回答
0
投票

您可以使用@HostBinding有条件地设置班级:

condition: boolean = true;

@HostBinding("class") private get hostClass(): string {
  return this.condition ? "redBorder" : "";
}

或用于特定的类名(例如redBorder):

@HostBinding("class.redBorder") private get redBorder(): boolean {
  return this.condition;
}

请参见以下两个演示:stackblitz 1stackblitz 2

© www.soinside.com 2019 - 2024. All rights reserved.