简体   繁体   English

Angular2-在点击时切换父级和子级样式

[英]Angular2 - toggle parent and children style on click

I am a complete noob with Angular2, so I apologize in advance if the question is very basic. 我对Angular2完全陌生,所以如果问题很基本,我会向您道歉。 So I have a side bar with an accordion menu. 所以我有一个带有手风琴菜单的侧栏。 I need the parent and the children to change their background color on click (so basically on expanding the section). 我需要父母和孩子在单击时更改其背景颜色(因此基本上是在扩展该部分)。 I understand that I can set an output on click on each div parent like so: 我知道我可以在每个div父级上单击时设置输出,如下所示:

(click)="toggleClass()"

What is not very clear to me is what do I put in the constructor, in toggleClass(). 我不太清楚的是我在toggleClass()中放入构造函数的内容。 I believe I need to somehow work with ngClass here, but I am a bit lost. 我相信我需要在某种程度上使用ngClass,但是我有点迷路。 See plunker here . 在这里看到朋克。

Thanks! 谢谢!

You can use NgClass Directive : https://angular.io/docs/ts/latest/api/common/index/NgClass-directive.html 您可以使用NgClass指令: https ://angular.io/docs/ts/latest/api/common/index/NgClass-directive.html

 export class NameComponent { booleanVariable: boolean; constructor() { } toggleClass(){ if(!this.booleanVariable) { this.booleanVariable = true; else { this.booleanVariable = false; } } } 
 .className { background: red; } 
 <button type="button" (click)="toggleClass()" class="btn btn-transparent" [ngClass]="{'className': booleanVariable}"data-toggle="collapse" data-target="#project-details">Projects</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM