0

Consider the following JSON structure: UPDATED:

[
    {
        "game0001": {
            "-JfuVKIsUBH27DMJfWmL": {
                "action": "STARTFIRSTHALF",
                "team": "HOME"
            },
            "-JfuVLJGMgclLZ0Maduh": {
                "action": "GOAL",
                "team": "AWAY"
            }
        },
        "$id": "events",
        "$priority": null
    },
    {
        "game0001": {
            "gameRunning": false,
            "startDate": "17/01/2015 17:27:42 PM"
        },
        "game0002": {
            "gameRunning": true,
            "startDate": "17/01/2015 19:45:59 PM"
        },
        "game0003": {
            "gameRunning": false,
            "scheduledDate": "17/01/2014 12:30:00 PM"
        },
        "$id": "games",
        "$priority": null
    }
]

How can I achieve filtering in AngularJS in HTML? In a very basic way, what I'm trying to achieve is the following:

<div ng-repeat="game in games">
  <div ng-repeat="event in events | filter:game">
    {{event.name}} - {{game.name}}
  </div>
</div>

I have 2 maps games and events which share the same keys, e.g (game0001, game0002)

While repeating the games, I would like to have a inner repeater for events and filter only the ones sharing the same key/id.

4
  • why not write a filter? Commented Jan 18, 2015 at 2:23
  • What are you asking...give more detail... Commented Jan 18, 2015 at 2:25
  • I just started exploring angular today, I'm not comfortable yet to write a filter Commented Jan 18, 2015 at 2:26
  • what is the question then? Commented Jan 18, 2015 at 2:27

1 Answer 1

1

Here's a working plunkr, I made assumptions about the data you wanted to fetch:

http://plnkr.co/edit/DVwQaRZeZiagGEzY4lCy?p=preview

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.games = {
    "games": {
      "game0001": {
        "first": "a",
        "second": "b"
      },
      "game0002": {
        "first": "c",
        "second": "d"
      }
    }
  }
  $scope.gamesKeys = Object.keys($scope.games['games']);

  $scope.events = {
    "events": {
      "game0001": {
        "event": {
          "key": "a"
        },
        "event": {
          "key": "b"
        },
        "event": {
          "key": "c"
        }
      },
      "game0002": {
        "event": {
          "key": "a"
        },
        "event": {
          "key": "b"
        },
        "event": {
          "key": "c"
        }
      }
    }
  }
  $scope.eventsKeys = Object.keys($scope.events['events']);
});

The important part is the ng-repeat here:

  <body ng-controller="MainCtrl">
    <div ng-repeat="gameKey in gamesKeys">

      <div ng-repeat="eventKey in eventsKeys">
        event: {{events['events'][eventKey].event.key}} - game: {{games['games'][gameKey].first}}
      </div>
    </div>
  </body>
Sign up to request clarification or add additional context in comments.

3 Comments

Can't get it to work right, it seems that the component I'm using is transforming the data a bit: paste.ofcode.org/KmAHaiHmVh4BRYED6Vjdtv
I have updated the question with the above json structure, it appears that angularFire modifies it slightly when fetching from Firebase
Thank you for your help, I've managed to find my way through using your suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.