0

我收到了来自服务器的对象列表,如下所示

[
    {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;
});

怎么了?

4

1 回答 1

1

试试这个解决方案。您可以只指定{$ : myPrecious.id}atfilter而无需担心层次结构级别,如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};
于 2016-09-15T13:44:33.413 回答