Angular: conditional class with *ngClass

后端 未结 19 1960
醉话见心
醉话见心 2020-11-22 05:21

What is wrong with my Angular code? I am getting:

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

相关标签:
19条回答
  • 2020-11-22 05:33

    We can make class dynamic by using following syntax. In Angular 2 plus, you can do this in various ways:

    [ngClass]="{'active': arrayData.length && arrayData[0]?.booleanProperty}"
    
    [ngClass]="{'active': step}"
    
    [ngClass]="step== 'step1'?'active':''"
    
    [ngClass]="step? 'active' : ''"
    
    0 讨论(0)
  • 2020-11-22 05:35

    That's the normal structure for ngClass is:

    [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>
    
    0 讨论(0)
  • 2020-11-22 05:35

    Not relevant with [ngClass] directive but I was also getting the same error as

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

    and I thought to be the error in my [ngClass] condition but it turned out the property I was trying to access in the condition of [ngClass] was not initialized.

    Like I had this in my typescript file

    element: {type: string};
    

    and In my [ngClass] I was using

    [ngClass]="{'active', element.type === 'active'}"
    

    and I was getting the error

    Cannot read property 'type' of undefined at...

    and the solution was to fix my property to

    element: {type: string} = {type: 'active'};
    

    Hope it helps somebody who is trying to match a condition of a property in [ngClass]

    0 讨论(0)
  • 2020-11-22 05:36

    <div class="collapse in " [ngClass]="(active_tab=='assignservice' || active_tab=='manage')?'show':''" id="collapseExampleOrganization" aria-expanded="true" style="">
     <ul> 	 <li class="nav-item" [ngClass]="{'active': active_tab=='manage'}">
    <a routerLink="/main/organization/manage" (click)="activemenu('manage')"> <i class="la la-building-o"></i>
    <p>Manage</p></a></li> 
    <li class="nav-item" [ngClass]="{'active': active_tab=='assignservice'}"><a routerLink="/main/organization/assignservice" (click)="activemenu('assignservice')"><i class="la la-user"></i><p>Add organization</p></a></li>
    </ul></div>

    Code is good example of ngClass if else condition.

    [ngClass]="(active_tab=='assignservice' || active_tab=='manage')?'show':''"
    
    [ngClass]="{'active': active_tab=='assignservice'}"
    
    0 讨论(0)
  • 2020-11-22 05:37

    While I was creating a reactive form, I had to assign 2 types of class on the button. This 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, button has btn and btn-class (from bootstrap), otherwise just btn class.

    0 讨论(0)
  • 2020-11-22 05:39

    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>
    
    0 讨论(0)
提交回复
热议问题