Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我在Angularjs中有一个过滤器,它返回“TypeError:无法读取未定义的属性'id'”_Javascript_Angularjs - Fatal编程技术网

Javascript 我在Angularjs中有一个过滤器,它返回“TypeError:无法读取未定义的属性'id'”

Javascript 我在Angularjs中有一个过滤器,它返回“TypeError:无法读取未定义的属性'id'”,javascript,angularjs,Javascript,Angularjs,这是我的过滤器: angular.module('App.filters', []).filter('categoryFilter', [function () { return function (clients, selectedCategory) { if (!angular.isUndefined(clients) && !angular.isUndefined(selectedCategory) && selectedCategory.lengt

这是我的过滤器:

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>

如果angular.equalsclient.category.id,id{在我看来,如果调用{{client.category.id}},那么错误就在这一行。

我认为这会很有帮助:

在过滤器中:

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">

可能是您缺少foreach中的键参数吗?sintax是angular.forEachcollection,functionvalue,key{…}你能在if语句之前使用console.log或debug检查client和client.category循环的每个迭代的值吗?向我们展示视图,你是如何调用过滤器的?虽然阅读这段代码并不困难,但请将其添加到你的帖子中,使其更具可读性。我同意mer10z_tech的观点。试着写一些关于o使用console.login的控制台如果我在console.logclient.category.id中写入,它将向我显示id,但有时会给我一个错误类型错误:无法读取未定义的属性“id”