2016-03-20 12 views
2

私のアプリケーションには、モーダルウィンドウに表示して循環させる独自の属性を持つ「プレーヤー」のセットがあります。 UIは次のようになります。anglejsの配列にxオブジェクトを表示

<!-- Score Round Modal --> 
    <div class="modal fade" id="myScoreModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" ng-repeat="player in players"> 
    <div class="modal-dialog" role="document"> 
     <div class="modal-content"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
      <h4 class="modal-title" id="myModalLabel">Score for {{player.data}}</h4> 
     </div> 
     <div class="modal-body"> 
... 
     </div> 
     <div class="modal-footer"> 
      <button type="button" class="btn btn-danger" data-dismiss="modal" ng-click="cancelRound()">Cancel</button> 
      <button type="button" class="btn btn-warning" ng-click="previousPlayer()">Previous</button> 
      <button type="button" class="btn btn-default" ng-click="nextPlayer()">Next Player</button> 
      <button type="button" class="btn btn-primary" data-dismiss="modal" ng-click="calculateRound()">Finish</button> 
     </div> 
     </div> 
    </div> 
    </div> 

ここで、プレイヤー(および後でいくつかの属性)を表示したい場合は、表示するファーストネームを取得できます。しかし、私は関数previousPlayer()nextPlayer()を使って循環したいと思います。 jsセクションでは、私が増加変数と減少がそうのようなcurrentPlayerと呼ばれています:

$scope.nextPlayer = function() { 
    // Cycles to next player cards within the round 
    $scope.currentPlayer++; 
    console.log($scope.currentPlayer); 
}; 


$scope.previousPlayer = function() { 
    // Cycle to previous player cards within the round 
    $scope.currentPlayer--; 
    console.log($scope.currentPlayer); 
}; 

目標はただ表示するだけでなく、playerモデルを修正しないことができるようになります。

+0

' ng-repeat = "player in player" '$ ng-repeat ="プレイヤーのトラックを$ index "$"で置き換えると、 'ng-if ="($ indexの条件)を実行することができます。 $ index = 'players'配列で使用されるインデックス。別の方法は、players_in_cycleという別の配列を作成し、その中の場所を入れ替えることです。 –

+0

だから 'ng-if ="(currentPlayer = $ index) "'は表現のように感じますが、どこに置くのですか? – Macness

+0

期待される動作は何ですか?表示されているデータを1つのモーダルとトグルするだけのようです。どのようなモーダルライブラリを使用していますか? – charlietfl

答えて

0

を削除するビューで$scope.player

$scope.currentPlayer = 0; 

function setPlayer() { 
    if ($scope.currentPlayer >= 0 && $scope.currentPlayer < $scope.players.length) { 
    $scope.player = $scope.players[$scope.currentPlayer]; 
    }else{ 
    alert('At end of players'); 
    } 
} 

setPlayer(); 

$scope.nextPlayer = function() { 
    // Cycles to next player cards within the round 
    $scope.currentPlayer++; 
    setPlayer(); 

}; 
$scope.previousPlayer = function() { 
    // Cycle to previous player cards within the round 
    $scope.currentPlayer--; 
    setPlayer(); 
}; 

新しいプロパティを設定します。

あなたのHTMLは、ビットを簡素化することができます - あなたはng-repeatng-ifを使用する必要はありません。代わりにplayers[currentPlayer]と現在のプレーヤーにアクセス:

<div class="modal fade" id="myScoreModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
    <div class="modal-dialog" role="document"> 
     <div class="modal-content"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
      <h4 class="modal-title" id="myModalLabel">Score for {{players[currentPlayer ].data}}</h4> 
     </div> 
     <div class="modal-body"> 
     ... 
     </div> 
     <div class="modal-footer"> 
      <button type="button" class="btn btn-danger" data-dismiss="modal" ng-click="cancelRound()">Cancel</button> 
      <button type="button" ng-if="currentPlayer > 0" class="btn btn-warning" ng-click="previousPlayer()">Previous</button> 
      <button type="button" ng-if="currentPlayer < players.length - 1" class="btn btn-default" ng-click="nextPlayer()">Next Player</button> 
      <button type="button" class="btn btn-primary" data-dismiss="modal" ng-click="calculateRound()">Finish</button> 
     </div> 
    </div> 
    </div> 
</div> 

ワーキングJsFiddle:http://jsfiddle.net/kxUR9/16/

+0

UIにいくつか調整を加えましたが、これは驚きです。文脈から少し離れているかもしれませんが、 "..."セクションではスコアリングが行われます。私がやる必要があることは、 'ng-model =" players [currentPlayer] .someScoreElement "'のようなものを追加して、それをそのプレーヤーに結びつけることです。 – Macness

+0

はい、そうです。 –

-1

1人のプレーヤーのみを表示する場合は、ng-repeatを使用すると意味がありません。

ただ単にあなたが一度に1人のプレーヤーのためのプレーヤーのデータを表示したいので、1つのモーダルウィンドウが十分であるべきng-repeat

関連する問題