ng-repeat中的ng-init仅显示最后一项信息

Alg*_*ron 1 angularjs angularjs-ng-repeat angularjs-ng-init

我想循环遍历这样的项目:

<section class="col-sm-4" data-ng-controller="PredictionsController" data-ng-init="findMyPredictions()">
    <div class="games">
        <div class="game-row" ng-repeat="prediction in predictions" ng-init="getGame(prediction.game_id)">
            <a class="button primary alt block" href="#!/predictions/{{prediction._id}}">
                <span class="home flag {{gameInfo.team1_key}}"></span>
        <span class="middle">
            <span class="date">{{gameInfo.play_at | date: 'd.MM HH:mm'}}</span>
            <span class="versus">{{gameInfo.team1_title}} - {{gameInfo.team2_title}}</span>
        </span>
                <span class="away flag {{gameInfo.team2_key}}"></span>
            </a>
        </div>
    </div>
</section>
Run Code Online (Sandbox Code Playgroud)

但输出只是相同信息的X倍: 在此输入图像描述 虽然请求正确完成: 在此输入图像描述

知道这里有什么问题吗?

更新:我的getGame功能:

$scope.getGame = function (game_id) {
    $scope.gameInfo = {};
    $http.get('/games/' + game_id)
    .success(function (data) {
        $scope.gameInfo = data;
    });

};
Run Code Online (Sandbox Code Playgroud)

dav*_*ave 7

gameInfo每次都要覆盖.因此,当它呈现时,它只是显示最后一次三次.你需要做的更像:

<div class="game-row" ng-repeat="prediction in predictions" ng-init="getGame(prediction)">
Run Code Online (Sandbox Code Playgroud)

注意我们传入的是prediction对象,而不仅仅是id.然后你可以这样做:

$scope.getGame = function (prediction) {
  prediction.gameInfo = {};
  $http.get('/games/' + game_id)
  .success(function (data) {
    prediction.gameInfo = data;
  });
};
Run Code Online (Sandbox Code Playgroud)

并且在你的html中,而不是gameInfo.whatever你会做prediction.gameInfo.whatever,这样每个预测都有它自己的变量,你不会覆盖你的变量的单个副本.

例如:

<span class="date">{{gameInfo.play_at | date: 'd.MM HH:mm'}}</span>
Run Code Online (Sandbox Code Playgroud)

会成为

<span class="date">{{prediction.gameInfo.play_at | date: 'd.MM HH:mm'}}</span>
Run Code Online (Sandbox Code Playgroud)