Home > Web Front-end > JS Tutorial > How to Avoid 'Cannot read property 'remove' of undefined' when Using Angular's *ngClass?

How to Avoid 'Cannot read property 'remove' of undefined' when Using Angular's *ngClass?

Linda Hamilton
Release: 2024-12-02 16:08:12
Original
301 people have browsed it

How to Avoid

Using Conditional Classes with *ngClass in Angular

This article addresses an issue encountered when utilizing the Angular ngClass directive to assign conditional classes. The error, "Cannot read property 'remove' of undefined," is triggered by an incorrect implementation of ngClass.

To rectify the issue, it's important to understand that Angular version 2 offers multiple approaches to conditionally apply classes:

Option 1:

[class.my_class] = "step === 'step1'"
Copy after login

Option 2:

[ngClass]="{'my_class': step === 'step1'}"
Copy after login

Option 3: Multiple Classes

[ngClass]="{'my_class': step === 'step1', 'my_class2': step === 'step2' }"
Copy after login

Option 4: Using Numbers as Keys

[ngClass]="{'1': 'my_class1', '2': 'my_class2', '3': 'my_class4'}[step]"
Copy after login

Option 5: Conditional Operator

[ngClass]="step === 'step1' ? 'my_class1' : 'my_class2'"
Copy after login

It's worth noting that the example you provided, using the setter syntax, is not a recommended approach. Instead, opt for one of the methods discussed above. By implementing these changes, you can effectively utilize *ngClass to apply conditional classes and avoid the "Cannot read property 'remove' of undefined" error.

The above is the detailed content of How to Avoid 'Cannot read property 'remove' of undefined' when Using Angular's *ngClass?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template