Can't assign [ngClass] to Angular components generated by *ngFor loop

user1675891

I'm trying to follow an example showing how to bind a class active to a component when it's clicked. When I execute the code based on the markup below

<div *ngFor="let menu of menus;"
     [id]="menu.id"
     (click)="onClick($event,menu.link)"
     [ngClass]="'active':menu.active"
     class="navigator">
  {{menu.title}}
</div>

I get the following error. NB - there's onClick(...) method in the component and at the moment I commented out all its contents. The error seems to be purely related to the markup (unless I need to declare something extra in the component, like an array or such). At least as far I've seen the examples while googlearching this issue.

Uncaught Error: Template parse errors:
Parser Error: Unexpected token ':' at column 9 in ['active':menu.active] in ng:///AppModule/NavigatorComponent.html@11:9 ("
[id]="menu.id"
(click)="onClick($event,menu.link)"
[ERROR ->][ngClass]="'active':menu.active"
class="navigator">
{{menu.title}}
"): ng:///AppModule/NavigatorComponent.html@11:9

What am I missing?

Günter Zöchbauer

'active':menu.active isn't a valid expression.

User either the object literal syntax

[ngClass]="{'active':menu.active}"

or the string syntax

[ngClass]="menu.active ? 'active' : null"

or

[class.active]="menu.active"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ngClass in a ngFor loop, angular 6

individual ngClass in a ngFor loop, angular 6

Angular, add unique class to ngFor loop while having an existing ngClass

Angular components ngClass

Angular: I have an NgFor with an Index of loop, how can I assign the name of an element to be "myName+IndexOfLoop"

Angular OnPush Change Detection Propagation to a Child Components in a ngFor Loop

How to correctly dynamically render components in angular in a ngFor loop

ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

Why can't I style my first word within my Angular ngFor loop?

Angular 2, using ngClass with conditionals in ngFor

ngClass used with ngFor in Angular to toggle icons

Change color of the clicked button with ngClass, on buttons generated with ngFor

Assign Raster band to variable name in Loop - "can't assign" error

How can I break from an *ngFor loop in Angular 4?

Can I apply a template to a object-variable of a ngFor - loop in Angular?

Angular - Can't assign null to File type

Entered text value is replicating in dynamic generated textbox through ngFor loop in angular 2

How To Show A Count Of Similar Results Generated from *ngFor Loop Angular 6?

Can't access Angular Components via URL

Refer to autocomplete inputs generated dynamically with ngFor loop

Angular 8 library with CommonModule (ngif, ngfor, ngclass, etc)

How to use [ngClass] in a *ngFor Angular component without a local index keeper?

Angular4 Exception: Can't bind to 'ngClass' since it isn't a known property of 'input'

ngclass to values ​that are fetched from a * ngFor Keyvalue They don't work

Angular: ternary operator in ngFor throw error Can't bind to 'ngFor' since it isn't a known property of 'div'

Angular.js: TrackBy doesn't prevent full *ngFor-generated list refresh

Can't use *ngIF inside *ngFor : angular2

can't render the Data in *ngFor but it already show in console in angular 7

Angular2 can't use ngModel in form repeated by ngFor