仅当情况为真时,我才尝试将一个类应用于组件标签。
如何将此主机转换为条件主机,以便在需要时应用所需的类?
@Component({
selector: 'k-sidebar',
host: {
class: '{{isChanged}}'
},
templateUrl: './ng-k-side-bar.component.html',
encapsulation: ViewEncapsulation.None
})
您可以使用@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 1,stackblitz 2。