如何在我的情况下设置类名

how to setup the class name in my case

本文关键字:设置 情况下 我的      更新时间:2023-09-26

我正在尝试在我的情况下组合ng类条件语句。

我有以下声明。

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

我收到解析错误。

Syntax Error: Token ':' is an unexpected 

我不确定如何解决这个问题。谁能帮我?多谢

请改用以下语法:

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

或者将您的逻辑放在一个函数中:

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

在控制器中:

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

仅供参考:从该函数中,您可以返回一个对象、一个类数组或一个字符串。