微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

css – 如何在角度应用程序中将类添加到父级?

我有一个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的更多信息.

原文地址:https://www.jb51.cc/css/217527.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。