首页 > 解决方案 > 2 @HostBinding Angular 中的 1 个成员

问题描述

使用我的组件时,我使用以下默认引导类。它工作得很好(角度6):

@Input()
@HostBinding('class.form-control')
hasFormControl: boolean = true;

但是,我想配置其他 HostBindings 以在相同变量为真时应用,有点像这样:

@Input()
@HostBinding('class.form-control')
hasFormControl: boolean = true;
@HostBinding('class.px-0') this.hasFormControl; // does not compile

如何正确地做到这一点?

标签: angulartypescript

解决方案


您可以对同一属性@HostBinding(...)多次应用以获得所需的结果:

@Input()
@HostBinding('class.form-control')
@HostBinding('class.px-0')
hasFormControl: boolean = true;

推荐阅读