通过映射键过滤Angular JS

时间:2015-01-18 02:16:01

标签: javascript angularjs

考虑以下JSON结构: 更新:

[
    {
        "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
    }
]

如何在HTML中实现AngularJS中的过滤? 以一种非常基本的方式,我想要实现的目标如下:

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

我有两张共享相同键的地图游戏事件,例如(game0001,game0002)

在重复游戏时,我希望有一个内部转发器用于事件,并只过滤那些共享相同密钥/ ID的转发器。

1 个答案:

答案 0 :(得分:1)

Herr是一名工作人员,我对您想要获取的数据做出了假设:

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

重要的部分是ng-repeat:

  <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>