Angular: conditional class with *ngClass

后端 未结 19 1929
醉话见心
醉话见心 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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]

提交回复
热议问题