1

I am trying to change the visibility of a CSS class .handle to 'hidden' after a button has been clicked. I tried to just insert a modified class by doing [ngClass]="'handle.hidden'" after the button has been toggled but this didn't work.

HTML File

<div class="expand-collapse">
 <button class="button-text title-text" (click)="toggleButton()">
  <span class="minimize"></span>
---> <div [ngClass]="'handle.hidden'"></div>
 </button>
</div>

CSS File

.handle {
    height: 25px !important;
    position: relative;
}

.handle.hidden {
    visibility: hidden;
}

.handle.visible {
    visibility: visible;
}
Jay Berd
  • 43
  • 1
  • 6
  • [The documentation](https://angular.io/api/common/NgClass#description) seems to imply that if you want to add multiple classes they should be space-separated. – Heretic Monkey Jul 30 '19 at 18:58
  • 3
    Use https://angular.io/api/common/NgIf `*ngIf`? – Fussel Jul 30 '19 at 18:59
  • Possible duplicate of [Adding multiple class using ng-class](https://stackoverflow.com/questions/18871277/adding-multiple-class-using-ng-class) – Heretic Monkey Jul 30 '19 at 19:02

5 Answers5

1

The problems is with the class name used in your ngClass binding. It must be:

<div [ngClass]="'handle hidden'"></div>

Or:

<div [ngClass]="{'handle': true, 'hidden': true}"></div>

In html code attribute class with value handle hidden match in css file as rule .handle.hidden

Sorry my english

1

Try this:

ngOnInit() {
   this.hidden = false;
}

toggleButton() {
   this.hidden = !this.hidden;
}
<div class="expand-collapse">
 <button class="button-text title-text" (click)="toggleButton()">
  <span class="minimize"></span>
   <div class="handle" [ngClass]="{'hidden': hidden}"></div>
 </button>
</div>
.handle {
    height: 25px !important;
    position: relative;
    &.hidden {
       visibility: hidden;
    }
}

No need to add class visible, as it's visible by default.

1

I would say there are two approaches. First is the conditional [ngClass] which means, in your toggleClass() function you are toggling the value of a boolean, like this.divIsVisible = !this.divIsVisible, then

<div class="handle" [ngClass]="divIsVisible ? 'visible' : 'hidden'"></div>

The second one is applying the *ngIf structural directory on the div, then

<div class="handle" *ngIf="divIsVisible"></div>

Also note that, *ngIf entirely removes the div from the DOM, while the visibility only hides it, but it's still on the page.

Patrik Alexits
  • 987
  • 9
  • 24
0

this is easier using class directives, just add the handle class normally if it's supposed to always be there and then:

<div class="handle" [class.hidden]="isHidden"></div>

where isHidden is your component variable that gets toggled. A "visible" class is redundant as it will become invisible / visible just by adding and removing the hidden class

bryan60
  • 28,215
  • 4
  • 48
  • 65
0

All you need is to add the hidden class conditionally while keeping track of the display status.

<button (click)="isHidden = !isHidden">
<div [class.hidden]="isHidden"></div>

In your .ts file, add and initialize isHidden:boolean.

Isaac Kbn
  • 91
  • 1
  • 3