我有下一个HTML
// This is parent <div class="some-class"> // This is child <totalizer</totalizer> </div>
如何改变孩子的父母风格(添加新课程)?
解决方法
您可以使用EventEmitter @Output()属性来指示父组件使用ngClass动态添加/删除css类.
在您的子累加器组件中,定义,
@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的更多信息.