是否可以在 Angular 2 中添加一个动态类来托管? [英] Is it possible to add a dynamic class to host in Angular 2?
本文介绍了是否可以在 Angular 2 中添加一个动态类来托管?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道在 Angular2 中,我可以通过执行以下操作将红色"类添加到组件的选择器元素中:
I know that in Angular2 I can add a class 'red' to a component's selector element by doing this:
@Component({
selector: 'selector-el',
host: {
'[class.red]': 'true'
},
...
})
我想知道是否有一种方法可以将 dynamic 类添加到主机,类似于您对 NgClass 所做的事情(我知道实际上不支持 NgClass,我正在寻找可能的解决方案):
I'm wondering whether there's a way to add a dynamic class to a host, similar to what you would do with NgClass (I know NgClass is not actually supported, I'm looking for possible solutions):
@Component({
selector: 'selector-el',
host: {
'[NgClass]': 'colorClass'
},
...
})
...
constructor(){
this.colorClass = 'red';
}
推荐答案
你可以这样使用:
@Directive({
(...)
host: {
'[class.className]' : 'className',
'[class]' : 'classNames'
}
}
export class MyDirective {
constructor() {
this.className = true;
this.classNames = 'class1 class2 class3';
}
}
这篇关于是否可以在 Angular 2 中添加一个动态类来托管?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文