Angular如何从输入文本中过滤Object.keys

时间:2016-04-29 14:13:58

标签: angularjs filter ng-repeat

我有一点问题,我有这个:

$scope.data = {
   parent1:[{
              data:[{name:'John'}]
              child:[{data:'something'}]
           }],
   parent2:[{
              data:[{name:'Charles'}]
              child:[{data:'something'}]
           }],
}

我想设置一个过滤器,但我只想过滤像"parent1, parent2"之类的Object.keys:

<input type="text" placeholder="Search" ng-model="myFilter">

<li ng-repeat="dt in data | filter : myFilter">

3 个答案:

答案 0 :(得分:2)

这可能会让你接近,但它只能找到完全匹配。

<html>

<body>

  <div ng-app="filterApp" ng-controller="filterDemo">

    <input type="text" placeholder="Search" ng-model="myFilter">

    <ul ng-repeat="(key, value) in data | filter : filterList">
      <li>{{key}} {{value}}</li>
    </ul>
  </div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script>
  angular.module("filterApp", []).controller("filterDemo", filterDemo)

function filterDemo($scope) {
  var foo = {
   parent1:[{
              data:[{name:'John'}],
              child:[{data:'something'}]
           }],
   parent2:[{
              data:[{name:'Charles'}],
              child:[{data:'something'}]
           }]
}

$scope.data = [];
for (var key in foo) {
  // must create a temp object to set the key using a variable
  var tempObj = {};
  tempObj[key] = foo[key];
  $scope.data.push(tempObj);
};
console.log($scope.data);



  $scope.filterList=function(object) {
    if ($scope.myFilter) {
     return object.hasOwnProperty($scope.myFilter)
    }
    return true;
  };
}

</script>

</body>

</html>

答案 1 :(得分:1)

你可以像这样绑定:

<li ng-repeat="(key,value) in data">
    {{key}}
</li>

答案 2 :(得分:0)

&#13;
&#13;
/product/10/issues/24
&#13;
var app = angular.module('app', []);

function TestCtrl($scope) {


  $scope.items = {
    "Auction Platform": [{
      "project_name": "Auction Platform",
      "version_name": "Auction version 1",
      "version_id": 248
    }],
    "Finansme": [{
      "project_name": "Finansme",
      "version_name": "Bug Fixes for External Users v1",
      "version_id": 147
    }]


  };
  $scope.keys = Object.keys($scope.items);
  $scope.values = Object.values($scope.items);
  //console.log($scope.keys);
  //console.log($scope.values);
}
&#13;
&#13;
&#13;