Angular: conditional class with * ngClass

What is wrong with my corner code? I get:

Cannot read property 'remove' of undefined at BrowserDomAdapter.removeClass ... 

HTML

 <ol class="breadcrumb"> <li *ngClass="{active: step==='step1'}" (click)="step='step1; '">Step1</li> <li *ngClass="{active: step==='step2'}" (click)="step='step2'">Step2</li> <li *ngClass="{active: step==='step3'}" (click)="step='step3'">Step3</li> </ol> 
+394
javascript css angular angular-template
Feb 08 '16 at 12:00
source share
13 answers

Angular 2, .., 7 provides several ways to conditionally add classes:

enter one

 [class.my-class]="step=='step1'" 

type two

 [ngClass]="{'my-class': step=='step1'}" 

and several options:

 [ngClass]="{'my-class': step=='step1', 'my-class2':step=='step2' }" 

type three

 [ngClass]="{1:'my-class1',2:'my-class2',3:'my-class4'}[step]" 

type four

 [ngClass]="(step=='step1')?'my-class1':'my-class2'" 
+811
Feb 01 '17 at 7:50
source share

[ngClass]=... instead of *ngClass .

* intended only for abbreviated syntax for structural directives, where you can, for example, use

 <div *ngFor="let item of items">{{item}}</div> 

instead of a longer equivalent version

 <template ngFor let-item [ngForOf]="items"> <div>{{item}}</div> </template> 

See also https://angular.io/docs/ts/latest/api/common/index/NgClass-directive.html

 <some-element [ngClass]="'first second'">...</some-element> <some-element [ngClass]="['first', 'second']">...</some-element> <some-element [ngClass]="{'first': true, 'second': true, 'third': false}">...</some-element> <some-element [ngClass]="stringExp|arrayExp|objExp">...</some-element> <some-element [ngClass]="{'class1 class2 class3' : true}">...</some-element> 

See also https://angular.io/docs/ts/latest/guide/template-syntax.html

 <!-- toggle the "special" class on/off with a property --> <div [class.special]="isSpecial">The class binding is special</div> <!-- binding to `class.special` trumps the class attribute --> <div class="special" [class.special]="!isSpecial">This one is not so special</div> 
 <!-- reset/override all class names with a binding --> <div class="bad curly special" [class]="badCurly">Bad curly</div> 
+407
Feb 08 '16 at 12:02 on
source share

Another solution would be to use [class.active] .

Example:

 <ol class="breadcrumb"> <li [class.active]="step=='step1'" (click)="step='step1'">Step1</li> </ol> 
+70
Feb 08 '16 at 12:22
source share

Normal structure for ngClass :

 [ngClass]="{'classname' : condition}" 

So in your case, just use it like this ...

 <ol class="breadcrumb"> <li [ngClass]="{'active': step==='step1'}" (click)="step='step1'">Step1</li> <li [ngClass]="{'active': step==='step2'}" (click)="step='step2'">Step2</li> <li [ngClass]="{'active': step==='step3'}" (click)="step='step3'">Step3</li> </ol> 
+56
Jul 20 '17 at 5:21
source share

with the following examples you can use "IF MORE"

 <p class="{{condition ? 'checkedClass' : 'uncheckedClass'}}"> <p [ngClass]="condition ? 'checkedClass' : 'uncheckedClass'"> <p [ngClass]="[condition ? 'checkedClass' : 'uncheckedClass']"> 
+37
Jun 08 '18 at 13:07 on
source share

You can use ngClass to apply the class name both conditionally and not in Angular

Example

 [ngClass]="'someClass'"> 

Conditional

 [ngClass]="{'someClass': property1.isValid}"> 

Multiple condition

  [ngClass]="{'someClass': property1.isValid && property2.isValid}"> 

Method expression

 [ngClass]="getSomeClass()" 

This method will be inside your component.

  getSomeClass(){ const isValid=this.property1 && this.property2; return {someClass1:isValid , someClass2:isValid}; } 
+30
Aug 04 '17 at 18:44 on
source share

You should use something ( [ngClass] instead of *ngClass ) as follows:

 <ol class="breadcrumb"> <li [ngClass]="{active: step==='step1'}" (click)="step='step1; '">Step1</li> (...) 

+12
Feb 08 '16 at 12:05
source share

In Angular 7.X

CSS classes are updated as follows, depending on the type of expression calculation:

  • string - added CSS classes listed in the string (separated by spaces)

  • Array - added CSS classes declared as Array elements.

  • Object-keys are CSS classes that are added when the expression specified in the value accepts the true value, otherwise they are deleted.

 <some-element [ngClass]="'first second'">...</some-element> <some-element [ngClass]="['first', 'second']">...</some-element> <some-element [ngClass]="{'first': true, 'second': true, 'third': false}">...</some-element> <some-element [ngClass]="stringExp|arrayExp|objExp">...</some-element> <some-element [ngClass]="{'class1 class2 class3' : true}">...</some-element> 
+6
. Dec 05 '18 at 14:24
source share

While I was creating the reactive form, I had to assign 2 class types to the button. Here is how I did it:

 <button type="submit" class="btn" [ngClass]="(formGroup.valid)?'btn-info':''" [disabled]="!formGroup.valid">Sign in</button> 

When the form is valid, the button has btn and btn-class (from bootstrap), otherwise just the btn class.

+5
Sep 06 '17 at 23:49
source share

to extend Mostaf Mashaekhi his answer to the second option> you can also link several options using ",",

 [ngClass]="{'my-class': step=='step1', 'my-class2':step=='step2' }" 

Also * ngIf can be used in some of these situations, usually in combination with * ngFor

 class="mats p" *ngIf="mat=='painted'" 
+5
May 7 '18 at 18:25
source share

Here is what worked for me:

 [ngClass]="{'active': dashboardComponent.selected_menu == 'profile'}" 
+3
Mar 23 '18 at 4:36
source share

ngClass syntax:

 [ngClass]="{'classname' : conditionFlag}" 

You can use like this:

 <ol class="breadcrumb"> <li [ngClass]="{'active': step==='step1'}" (click)="step='step1'">Step1</li> <li [ngClass]="{'active': step==='step2'}" (click)="step='step2'">Step2</li> <li [ngClass]="{'active': step==='step3'}" (click)="step='step3'">Step3</li> </ol> 
+3
Jun 19 '19 at 5:53 on
source share

Let YourCondition be your condition or logical property, then do like this

 [class.yourClass]="YourCondition" 
+2
Mar 20 '19 at 6:15
source share



All Articles