0
我旁边HTML如何在角度应用程序中向父级添加类?
// This is parent
<div class="some-class">
// This is child
<totalizer</totalizer>
</div>
我怎样才能改变从独生子女父母风格(添加新的类)?
我旁边HTML如何在角度应用程序中向父级添加类?
// This is parent
<div class="some-class">
// This is child
<totalizer</totalizer>
</div>
我怎样才能改变从独生子女父母风格(添加新的类)?
您可以使用EventEmitter
@Output()
属性,该属性用信号通知父组件使用ngClass
动态地添加/删除css
类。
在你的孩子totalizer
组件,定义,
@Output() cssRefresh = new EventEmitter<boolean>();
//when you need to add/remove css emit an event out to the parent like this
// (preferably in a method in this component),
this.cssRefresh.emit(true); // or 'false' depending on add/remove
父
html
修改此
然后,
<div class="some-class" [ngClass]="{ 'dynamicClass1 dynamicClass2 dynamicClass3': addCss}">
// This is child
<totalizer (cssRefresh)=refreshCss($event)></totalizer>
</div>
父组件内部添加此方法和属性,
addCss = false; // set 'initial state' based on your needs
refreshCss(add: boolean) {
this.addCss = add ? true : false;
}
更多关于ngClass
here。