[英]How to filter a list in angular js by sublevels of object
我從服務器收到了類似這樣的對象列表
[
{id: 'a', checked :true,test:{sub:{var:4}}},
{id: 'b', checked:true,test:{sub:{var:3}}},
{id: 'c', checked:true,test:{sub:{var:2}}},
{id: 'd', checked:true,test:{sub:{var:2}}},
{id: 'e', checked:true,test:{sub:{var:3}}}
]
現在,我想基於“ var”過濾此列表。 我正在做類似的事情:
<div ng-controller="repeatCtrl">
<div ng:repeat="o in list | filter: {test.sub.var : myPrecious.id}">
{{o.id}}:
<input type="checkbox" ng:model="o.checked" />
{{o.checked}}
</div>
</div>
我的控制器代碼就像
var myApp = angular.module('myApp',[]);
myApp.controller('repeatCtrl', function($scope) {
$scope.list = [
{id: 'a', checked :true,test:{sub:{var:4}}},
{id: 'b', checked:true,test:{sub:{var:3}}},
{id: 'c', checked:true,test:{sub:{var:2}}},
{id: 'd', checked:true,test:{sub:{var:2}}},
{id: 'e', checked:true,test:{sub:{var:3}}}
];
$scope.myPrecious = 2;
});
怎么了?
試試這個解決方案 。 您可以只在filter
指定{$ : myPrecious.id}
,而不必擔心層次結構級別,如docs.angularjs.org所述: 可以使用特殊屬性名稱(默認為$)(例如,在{$:“ text”}中) )接受與對象的任何屬性或其嵌套對象屬性的匹配 :
HTML:
<div ng-repeat="o in list | filter : {$ : myPrecious.id}">
{{o.id}}
<input type="checkbox" ng-model="o.checked" />
{{o.checked}}
</div>
Javascript:
$scope.list = [
{id: 'a', checked:true,test:{sub:{var:4}}},
{id: 'b', checked:true,test:{sub:{var:3}}},
{id: 'c', checked:true,test:{sub:{var:2}}},
{id: 'd', checked:true,test:{sub:{var:2}}},
{id: 'e', checked:true,test:{sub:{var:3}}}
];
$scope.myPrecious = {id:2};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.