0

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

  • In this way, you can change parents style ( add a new class ) from child [How to add class to parent in the angular application?](https://stackoverflow.com/questions/46241271/how-add-class-to-parent-in-angular-application) – huzaifamandviwala Aug 09 '19 at 10:46

2 Answers2

1

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"

Sam
  • 1,736
  • 8
  • 15
0

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