I'm trying to build a basic tic tac toe game in angular 1. When I click on a square I want to either add X or O depending on the turn. I cannot figure out how to do that. I can do it in jquery but I want to do it in angular.
<div class="squares-container" ng-repeat="square in squares">
<div class="s" id="{{ $index + 1 }}" ng-click="move(square)", ng-bind="{{ $index + 1 }}"></div>
</div>
angular
.module('app')
.directive('ticTacToe', function(){
return {
restrict: 'E',
templateUrl: 'tic-tac-toe.html',
controller: controller
};
function controller($scope){
function init() {
let turn = 0;
let currentPiece = 'X';
$scope.squares = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$scope.move = function(id){
currentPiece = turn % 2 === 0 ? 'X' : 'O';
$scope.id = currentPiece;
turn++;
};
}
init();
}
})
Here's the code. https://plnkr.co/edit/UwHsltXVLFAVG6pHKKwO?p=preview