我有ng-repeat
:
<div ng-repeat="num in arr" ng-class="selected(num.id) ? 'active' : ''">
<span>{{num.name}}</span>
</div>
还检查元素是否处于活动状态的方法selected()
:
$scope.selected = function(num){
angular.forEach($scope.categorySelect, function (value, key) {
if(value.id == num){
// Here works console.log()
return ($scope.categorySelect[key].checked == true) ? false : true;
}
})
}
但对我来说它不起作用。
答案 0 :(得分:3)
ng-class
的语法应如下所示:
<div ng-repeat="num in arr" ng-class="{ 'active': selected(num.id) }">
此外,$scope.selected()
也不会返回任何内容。您在匿名函数中返回一个布尔值,它会破坏forEach
。
像这样更改你的功能
$scope.selected = function (num) {
var isSelected = false;
angular.forEach($scope.categorySelect, function (value, key) {
if(value.id == num){
isSelected = $scope.categorySelect[key].checked;
return;
}
});
return isSelected;
}
答案 1 :(得分:2)
应该是:
<div ng-repeat="num in arr" ng-class="{'active': selected(num.id) }">
<span>{{num.name}}</span>
</div>
答案 2 :(得分:0)
您的类语法错误:
ng-class="{'active' : selected(num.id)}"