FlyingCat
FlyingCat

Reputation: 14250

how to setup the class name in my case

I am trying to combine the ng-class condition statements in my case.

I have the following statements.

<div ng-class="item.new ? 'newItem' : 'oldItem'; 'discount' : item.getType === true && item.getSave === true">{{item.name}}</div>

I am getting the parsing error.

Syntax Error: Token ':' is an unexpected 

I am not sure how to fix this. Can anyone help me about it? Thanks a lot

Upvotes: 0

Views: 32

Answers (1)

floribon
floribon

Reputation: 19183

Use this syntax instead:

<div ng-class="{newItem: item.new, oldItem: !item.new, discount: item.getType === true && item.getSave === true}">

Or alternatively put your logic in a function:

<div ng-class="getClasses(item)">

And in your controller:

$scope.getClasses = function(item) {
  return {
    newItem: item.new,
    oldItem: !item.new,
    discount: item.getType === true && item.getSave === true
  };
}

FYI: from that function you can return an object, or an array of classes, or a string.

Upvotes: 1

Related Questions