2017-09-15 34 views

回答

3

您可以使用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; 
} 

更多关于ngClasshere

相关问题