【发布时间】:2017-08-10 09:55:26
【问题描述】:
我正在尝试构建井字游戏,但在样式方面存在问题。我有一个ng-repeat 用于所有 9 个方格,并且需要在每 3 个方格之后添加一个 div,以便可以构建下一行。我按照文档中的说明使用ng-repeat-end,但不确定我做错了什么。
<p>Turn: <span ng-bind="vm.turn"></span></p>
<div class="container" ng-repeat="square in vm.squares track by square.id">
<div class="square" ng-bind="square.piece" ng-click="vm.move(square.id)"></div>
</div>
<div ng-repeat-end class="row-divider" ng-if="!(square.id % 3)"></div>
<div>
<button class="btn btn-primary" click="newGame()">New game</button>
.square {
border: 1px solid #000;
width: 50px;
height: 50px;
float: left;
}
.row-divider {
clear: both;
display:block;
}
angular
.module('app')
.directive('ticTacToe', function(){
return {
restrict: 'E',
templateUrl: 'tic-tac-toe.html',
controller: TicTacToeController,
controllerAs: 'vm'
};
TicTacToeController.$inject = [];
function TicTacToeController(){
var vm = this;
vm.turn = 0;
let currentPiece = 'X';
vm.squares = [
{ id: 1, piece: null },
{ id: 2, piece: null },
{ id: 3, piece: null },
{ id: 4, piece: null },
{ id: 5, piece: null },
{ id: 6, piece: null },
{ id: 7, piece: null },
{ id: 8, piece: null },
{ id: 9, piece: null }
];
vm.move = function(squareId) {
console.log('click');
let index = squareId - 1;
currentPiece = vm.turn % 2 === 0 ? 'X' : 'O';
vm.squares[index].piece = currentPiece;
vm.turn++;
};
vm.newGame = function(){
vm.turn = 0;
}
};
})
【问题讨论】:
-
我打开了 plunker 并向我展示了预期的 soduko 模型.. 有什么问题
-
没关系,我打开了一个小窗口..
标签: javascript css angularjs angularjs-ng-repeat