我在Angularjs中有一个过滤器,它返回"TypeError: Cannot read property

I have a filter in Angularjs and it returned "TypeError: Cannot read property 'id' of undefined"

本文关键字:TypeError quot Cannot property read 返回 Angularjs 有一个 过滤器 我在      更新时间:2023-09-26

这是我的过滤器:

angular.module('App.filters', []).filter('categoryFilter', [function () {
return function (clients, selectedCategory) {
    if (!angular.isUndefined(clients) && !angular.isUndefined(selectedCategory) && selectedCategory.length > 0) {
        var tempClients = [];
        angular.forEach(selectedCategory, function (id) {
            angular.forEach(clients, function (client) {
                if (angular.equals(client.category.id, id)) {
                    tempClients.push(client);
                }
            });
        });
        return tempClients;
    } else {
        return clients;
    }
};
}]);

在控制器中我有一个函数:

$scope.setSelectedClient = function () {
var id = this.category.id;
if (_.contains($scope.selectedCategory, id)) {
    $scope.selectedCategory = _.without($scope.selectedCategory, id);
} else {
    $scope.selectedCategory.push(id);
}
return false;

};

和json:

[
{
   "id":"1",
   "name": "client1",
   "image":"images/client1.jpg",
   "category": {
       "id": "2",
       "designation": "Web"
    }
},
{
   "id":"2",
   "name": "client2",
   "image":"images/client2.jpg",
   "category": {
       "id":"1",
       "designation": "design"
    }
}]

视图:

<ul class="clients-images col-md-12 col-lg-12">
<li class="col-md-3 col-lg-3" data-ng-repeat="client in filtered = (clients | categoryFilter:selectedCategory) | offset: clientsPerPage*currentPage | limitTo: clientsPerPage">
   <a href="#projects/{{client.id}}"><img ng-src="{{client.image}}" width="210px" />
      <p>{{client.category.designation}}</p></a>
 </li>
</ul>

错误出现在这行"if (angular.equals(client.category. net) "Id, Id)){"。如果我调用{{client.category。

我想这会很有帮助:

在过滤器:

angular.module('App.filters', []).filter('categoryFilter', [function () {
return function (clients, args) {
    if (!angular.isUndefined(clients) && !angular.isUndefined(args.selectedCategory) && args.selectedCategory.length > 0) {
        var tempClients = [];
        angular.forEach(args.selectedCategory, function (id) {
            angular.forEach(clients, function (client) {
                if (angular.equals(client.category.id, id)) {
                    tempClients.push(client);
                }
            });
        });
        return tempClients;
    } else {
        return clients;
    }
};
}]);
HTML:

<li class="col-md-3 col-lg-3" 
data-ng-repeat="client in clients 
| categoryFilter:{selectedCategory: selectedCategory} 
| offset: clientsPerPage*currentPage 
| limitTo: clientsPerPage">