AngularJS ng-repeat过滤器应用于子节点

4

I have the following JSON data structure

{ "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上实现,或者我需要考虑其他解决方案?

4
尝试使用筛选器:filter: {status: {Status: 'Appointment booked'}} - Italo Ayres
非常好,谢谢。我已经为这个问题烦恼了一整天。如果您将其作为答案发布,我会将其标记为正确答案 :) - Janbango
make this an answer - marcusshep
1个回答

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'}}"

同样适用


我已经使用了@Italo Ayres评论中的解决方案,但我将把这个答案标记为正确的,因为它让我了解了自定义过滤器的灵活性。 - Janbango

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接