Janbango Janbango - 5 months ago 20
AngularJS Question

AngularJS ng-repeat filter to child node

I have the following JSON data structure

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


and I am trying to use an ng-repeat with a filter to the second level node status.Status. I have tried the following but the filter does not work.

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


Is the above possible on ng-repeat or do I need to look at an alternative solution?

Answer

You can always use a custom filter when all else fails:

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

As mentionned in comments

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

works aswell