我需要使用 ng-class 添加两个类,一个类我通过函数获得,另一个我需要通过表达式获得 -
下面是我正在使用但遇到一些语法错误的代码 -
<div class="field-icon" ng-class="getFieldClass(entry.entry_type_id) , 'used': entry_map[entry.guid] > 0" ></div>
如果可以同时使用函数和表达式,什么是正确的语法。
最佳答案
你错了ng-class
语法,它应该是 JSON 格式,如 ng-class="{'used': expression2 }"
, 表达式将返回 Boolean
基于该类将从类中添加或删除 attribute
值(value)。
作为您的 getFieldClass
方法正在返回类名,然后您可以在 getFieldClass
中移动您的两个类逻辑方法
标记
<div class="field-icon"
ng-class="getFieldClass(entry)" ></div>
代码
$scope.getFieldClass = function(entry){
//use entry.entry_type_id here to decide class which is first
//'text-box-icon' class has been selected on some condition based on entry.entry_type_id
return {"text-box-icon": true, 'used': $scope.entry_map[entry.guid] > 0};
}
关于angularjs - 带有函数和表达式的 ng-class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30530855/