简体   繁体   中英

How to add class for parent element buy clicking child button Angular 8

I want to add a class for parent element by clicking the child button

some text..... title text +
  <div class="collapseBox border mb-2" [ngClass]="{active: active2}"> <div class="collapse_inner"> some text.... </div> <div class="header py-2 px-2"> <h5 class="mb-0">title text</h5> </div> <button (click)="active1!= active1">+</button> </div> 

I want to collapse the targeted div by clicking the button, I am trying to add active class to parent div when clicking button

Make your ngClass a ternary and define your current class in ngClass.

<div [ngClass]="active ? 'collapseBox border mb-2 active' : 'collapseBox border mb-2'">

Also your click assignment logic is not correct, try

(click)="active = !active"

I m doing the click function wrongly it should be. (click)="active1 = !active1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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