Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.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中使用嵌套对象按对象搜索_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 如何在AngularJS中使用嵌套对象按对象搜索

Javascript 如何在AngularJS中使用嵌套对象按对象搜索,javascript,html,angularjs,Javascript,Html,Angularjs,我想按搜索对象筛选我的订单集合。希望在视图中仅显示匹配的订单 我有订单数组集合,如: $scope.orders = [{ "_id" : "56461e2b7caaf49345076709", "customer" : {"_id": "76461e2b7caaf49345076a19b", "name": "cr1"}, "seller" : {"_id": "96461e2b7caaf49345076a18b", "name": "sl1"}, "ad

我想按搜索对象筛选我的订单集合。希望在视图中仅显示匹配的订单

我有订单数组集合,如:

$scope.orders = [{ 
    "_id" : "56461e2b7caaf49345076709", 
    "customer" : {"_id": "76461e2b7caaf49345076a19b", "name": "cr1"},
    "seller" : {"_id": "96461e2b7caaf49345076a18b", "name": "sl1"}, 
    "address" : "Squire Park",
    "qt" : 5
},
{ 
    "_id" : "56461e2b7caaf49345076708", 
    "customer" : {"_id": "76461e2b7caaf49345076a19b1", "name": "cr2"},
    "seller" : {"_id": "96461e2b7caaf49345076a18c1", "name": "sl2"}, 
    "address" : "Squire Park1", 
    "qt" : 6
},
..................
];
以及我的搜索对象,如:

$scope.search = {
    "qt": 5,
    "customer" : {"_id": "76461e2b7caaf49345076a19b1"},
    "seller" : {"_id": "96461e2b7caaf49345076a18c1"}
};
视图:


{{顺序.\u id}}
{{order.customer.name}
我的myFilter功能应该是什么


感谢您的帮助。

一个相当简单的解决方案是在示波器上附加一个过滤功能

$scope.orderFilter = function(order) {
  // implementation depends on how you want the search to behave
  // for example, if the order should match every property of the searchObj
  var pass = true;
  for (var property in $scope.search) {
    if ($scope.search.hasOwnProperty(property)) {
      if($scope.search[property] != order[property]) {
        pass = false;
      }
    }
  }
  return pass;
  // .. though i wouldnt recommend this implementation
  // you´d be better off by implementing a more specific matching
}
在我看来

<tr data-ng-repeat="order in orders | filter:orderFilter">
  <td>{{order._id}}</td>
  <td>{{order.customer.name}}</td>
</tr>
注意

如果更改搜索对象的属性,则不会再次调用过滤器函数,因为角度过滤器不会侦听嵌套属性的更改。仅当您更改整个搜索对象时,才会调用筛选器

<tr data-ng-repeat="order in orders | filter:orderFilter">
  <td>{{order._id}}</td>
  <td>{{order.customer.name}}</td>
</tr>
angular.module('app').filter('myFilter', function() {
  return function(orders, searchObj) {
    var filteredOrders = [];
    orders.forEach(function(order) {
      // evaluate if you want to have the order show up based on the searchOjb
      // .. if so, add it to the array like so:
      filteredOrders.push(order);
    });
    return filteredOrders;
  }
});