Mercer
Mercer

Reputation: 9986

Define any conditional in a ternary operator

I want to change the colors of my cell, so in my table i do this on my td

data-ng-class="{selected.id == price.id && !price.isMinPrice ? 'selected' : '', selected.id == price.id && price.isMinPrice ? 'minSelected' : ''}"

i have this error:

Error: [$parse:syntax] Syntax Error: Token '.' is unexpected, expecting [:] at column 10 of the expression [{selected.id == price.id && !price.isMinPrice ? 'selected' : '', selected.id == price.id && price.isMinPrice ? 'minSelected' : ''}] starting at [.id == price.id && !price.isMinPrice ? 'selected' : '', selected.id == price.id && price.isMinPrice ? 'minSelected' : ''}].

What is wrong ..?

Upvotes: 0

Views: 120

Answers (2)

JLRishe
JLRishe

Reputation: 101652

You are using ng-class all wrong and that is why you are getting syntax errors.

You need to give it an object literal:

data-ng-class="{selected: selected.id == price.id && !price.isMinPrice,
                minSelected: selected.id == price.id && price.isMinPrice}"

This is also much cleaner than what you were trying to do.

Upvotes: 2

Rasalom
Rasalom

Reputation: 3111

I think that ng-class expects structure like this: {'class-name' : booleanValue}, and if value is true, class will be applied.

So in your case:

data-ng-class="{'selected' : selected.id == price.id && !price.isMinPrice, 'minSelected' : selected.id == price.id && price.isMinPrice}"

and if you want to use ternaty operator, you can use class attribute with {{}}:

class="{{selected.id == price.id && !price.isMinPrice ? 'selected' : ''}}"

Upvotes: 1

Related Questions