12

HTML:

<div *ngFor="let record of lift" class="list-lifts" [class.showLifts]="isBtnActive">

Component:

isBtnActive: boolean = false;

toggleClass() {
    this.isBtnActive = !this.isBtnActive;
  }

CSS:

.list-lifts {

  &:not(:first-of-type) {
    margin-top: -11px !important;
    display: none;
  }
}

.showLifts {
  display: block !important;
}

// I need something like this to be built in the view:
.ValueshowLifts {}

The toggleClass() function toggles the CSS class .showLifts on the click of a button. This works great.

The issue is, I need the class .showLifts to have a dynamic name and I'm not sure what the syntax is to produce it. For logical reasons, here's an example:

[class.{{ record.name }}showLifts]="isBtnActive"

But of course this isn't valid syntax.

2
  • 1
    [ngClass]="{[record.name]: true}" Commented Jun 17, 2017 at 7:30
  • Hint from user663031 is reasonable, but won't compile at '['. The expression has to be moved to a function, then it works Commented Apr 9, 2019 at 13:15

4 Answers 4

10

As others have mentioned you can use [ngClass]. An answer was posted here:

Dynamic classname inside ngClass in angular 2

But I wanted to mention that if you're using CSS to only hide or show elements, then instead you can use *ngIf or [hidden], depending if you want the element in the DOM or not.

Sign up to request clarification or add additional context in comments.

Comments

7

You can leverage ngClass directive here

[ngClass]="showLiftsClass"

Inside your code you can dynamically add css classes to it as follows,

showLiftsClass = {
      'record1showLifts' : true,
      'record2showLifts' : false,
      'record3showLifts' : false,
      'record4showLifts' : false
      ...
}

You can have single or multiple classes as a true. Value true will add the class to the DOM element.

Comments

3
<div class="form-group label-floating" 
    [ngClass]="{'is-empty': true, 'second-class' : true}"></div>

Comments

-1

Try this:

<div *ngFor="let record of lift; ; let i=index" class="list-lifts" 
[ngClass]="{'showLifts': isBtnActive}">

Or you can Use: *ngIf

*ngIf="expression" (e.g. *ngIf="i!=0" or *ngIf="i==0")

5 Comments

The class toggling works, but I'm still stuck at square one with the issue. I need the class .showLifts to have an appended string such as {{ record.name }}.
like this? <span [ngClass]="{'showLifts': isBtnActive}">{{record.name}}</span>
or you can not want toggling for string then, <span [ngClass]="{'showLifts': true}">{{record.name}}</span> or <span [ngClass]="{'showLifts': false}">{{record.name}}</span>
That won't work, let me try to explain it better: These divs are being built dynamically using *ngFor, and they are split into 4 categories. I'm hiding everything except the first-child of each category using display:none. onClick, a CSS class with display: block gets added to show the children. I need the CSS class to be dynamic because without it, every category becomes visible by clicking any button.
you can use index of record, i have updated anwer. by perticular index you can hide or show element. like [ngClass]="{'list-lifts': i==0}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.