1
votes

I am trying to add multiple classes in ngClass, where one of the classes is a concatenation of a literal string and a variable. This is what I need, but I got a syntax error:

<div [ngClass]="[{'w-' + percentage}, {'finished': true}]"></div>

Where is the error in this expression?

2
<div [ngClass]="{'w-' + percentage: true, 'finished': true}"></div> - enno.void
Parser Error: Missing expected : at column 7 in [{'w-' + percentage: true, 'finished': true}] - Miomir Dancevic
remove [] brackets from [{'w-' + percentage: true, 'finished': true}], only curly braces allowed. - Yogendra Chauhan
Parser Error: Missing expected : at column X in [{'w-' + percentage, 'finished': true}] - Miomir Dancevic
please write like @enno.void suggested. - Yogendra Chauhan

2 Answers

1
votes
<div [ngClass]="'w-' + percentage" [class.finished]="true/false"></div>

or if class finished is not connected to any variable then:

<div class="finished" [ngClass]="'w-' + percentage"></div>
0
votes

Write like this, It will resolve your problem.

<div [ngClass]="{'w-' + percentage: true, 'finished': true}"></div>