angular - 有条件地将类应用于组件主机
问题描述
仅当某些情况属实时,我才尝试将类应用于组件标记。
如何将此主机变成条件主机,以便在需要时应用所需的类?
@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。
推荐阅读
- javascript - 从数组中的项目计算一个数字,然后使用 Array.prototype.reduce() 获得这些数字的总和?(Javascipt)
- javascript - 通过过滤它的嵌套大小变体返回数组对象
- node.js - 在 linux manjaro 上安装 gulp-sass 时出错
- c# - 属性是否在使用 FlientValidation 的表达式 x => x.AccountNumber.ToString() 中丢失了元数据?
- javascript - 我想用 .sort() 重新列出我的状态,但 React.js 没有任何变化
- azure - 缺少 Azure Maven 任务输出
- python - 异常机制在一段时间后不起作用
- postgresql - 如何在 Lambda 函数中使用 Powershell 连接到 RDS PostgreSQL 数据库?
- colors - Vega Lite:条形图的色标
- python - 电影推荐系统:为每部电影找到评分者评分的电影