gpt4 book ai didi

angular - 有条件地将类应用于组件主机

转载 作者:行者123 更新时间:2023-12-05 01:12:45 25 4
gpt4 key购买 nike

我尝试仅在某些情况为真时才将类应用于组件标签。

我如何才能将此主机变成条件主机,以便在需要时应用所需的类?

@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 hasRedBorder(): boolean {
return this.condition;
}

查看以下两个演示:stackblitz 1 , stackblitz 2 .

关于angular - 有条件地将类应用于组件主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61594980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com