AngularJS ng-repeat过滤到子节点

时间:2016-07-06 20:35:37

标签: javascript angularjs

我有以下JSON数据结构

{ "firstName" : "John",
  "role" : "Manager",
  "status" : {
                "Id" : 30,
                "Status" : "Appointment booked",
                "statusDate" : 1467826508775
             },
  "surname" : "Smith",
  "title" : "Mr",
}

我试图将带有过滤器的ng-repeat用于第二级节点status.Status。我尝试了以下但过滤器不起作用。

ng-repeat="data in data | filter: {status.Status: 'Appointment booked'}"

以上是否可以在ng-repeat上进行,还是需要查看其他解决方案?

1 个答案:

答案 0 :(得分:2)

当其他所有方法都失败时,您始终可以使用自定义过滤器:

ng-repeat="d in data | myFormat"

JS:

app.controller('myCtrl', function ($scope) {
    $scope.data = {
        "firstName": "John",
        "role": "Manager",
        "status": {
            "Id": 30,
            "Status": "Appointment booked",
            "statusDate": 1467826508775
        },
        "surname": "Smith",
        "title": "Mr",
    }
});

app.filter('myFormat', function () {
    return function (x) {
        if (x.status.Status == 'Appointment booked') {
            return x;
        }
    };
});

正如评论中提到的

ng-repeat="d in data | filter : {status: { Status : 'Apointment booked'}}"

也是如此