2016-04-29 7 views
2

は、私はこれがあります。角度、私は少し問題を抱えている

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

を、私はそれにフィルタを設定したいが、私は同じようObject.keysをフィルタリングします"parent1, parent2" from:

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

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

答えて

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

あなたはこのようにバインドすることができます。

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

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); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script> 
 

 
<body ng-app="app"> 
 
    <div ng-controller="TestCtrl"> 
 
    <input style="width:70%" type=text ng-model=search placeholder="Type key names which are either 'Auction Platform' or 'Finansme' "> 
 
    <div ng-repeat="item in keys | filter:search"> 
 
     {{items[item][0].version_id}} | {{items[item][0].version_name}} 
 
    </div> 
 
    </div> 
 
</body>