javascript - 如何在不使用 jquery 的情况下使用 Angular 在 div 中添加内容

标签 javascript jquery angularjs angularjs-ng-repeat tic-tac-toe

我正在尝试以 Angular 1 构建一个基本的 tic tac toe 游戏。当我单击一个正方形时,我想根据回合添加 X 或 O。我不知道该怎么做。我可以用 jquery 来做,但我想用 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();
    }
})

这是代码。 https://plnkr.co/edit/UwHsltXVLFAVG6pHKKwO?p=preview

最佳答案

您可以将方 block 设置为对象并简单地更改它们的属性。

当您单击一个正方形时,将其传递给您的 move 函数,找到它的索引并更改该正方形的 piece 属性。

(function() {

  'use strict';

  angular.module('app', []);

})();

(function() {

  'use strict';

  angular.module('app').directive('ticTacToe', TicTacToeDirective);

  function TicTacToeDirective() {

    return {
      restrict: 'E',
      templateUrl: 'tic-tac-toe.html',
      controller: TicTacToeController,
      controllerAs: 'TicTacToeCtrl'
    };

  }

  TicTacToeController.$inject = [];

  function TicTacToeController() {

    // reference this to make it available in the view using the controller as syntax
    var vm = this;

    // expose our move function to the view
    vm.move = move;

    // we can even expose our newGame function to start a new game
    vm.newGame = newGame;

    // set our defaults - we will add turn to this controller so we can display it in the view 
    vm.turn = 0;
    var currentPiece = "X";

    // start a new game
    newGame();

    function move(square) {

      // get the index of the square passed in
      var index = vm.squares.indexOf(square);

      // set the squares piece property to the correct piece if it doesn't already have one set

      if (!vm.squares[index].piece) {

        vm.squares[index].piece = vm.turn % 2 === 0 ? 'X' : 'O';

        // increment the number of turns
        vm.turn++;

      }

    }

    function newGame() {

      // reset turn
      vm.turn = 0;

      // reset current piece
      currentPiece = "X";

      // setup our squares
      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
        }
      ];
    }

  }

})();
/* Styles go here */

.container {
  background-color: #14bdac;
}

.s {
  border: 1px solid #000;
  width: 50px;
  height: 50px;
  float: left;
}

.divider {
  display: block;
  clear: both;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>

<div ng-app="app">

  <tic-tac-toe></tic-tac-toe>

  <script type="text/ng-template" id="tic-tac-toe.html">

    <button ng-click="TicTacToeCtrl.newGame()">New game</button> Turns taken: {{TicTacToeCtrl.turn}}

    <hr>

    <div class="squares-container" ng-repeat-start="square in TicTacToeCtrl.squares track by $index">
      <div class="s" id="{{::square.id}}" ng-click="TicTacToeCtrl.move(square)">
        {{::square.id}}
        <span class="checked">{{square.piece}}</span>
      </div>
    </div>
    <div ng-repeat-end class="divider" ng-if="!(square.id % 3)"></div>

  </script>

</div>

关于javascript - 如何在不使用 jquery 的情况下使用 Angular 在 div 中添加内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42879889/

相关文章:

javascript - 在编写去抖动时,fn.apply(context, args) 的意义何在?

javascript - 当两行与另一列的总和相同时返回数组中的单行

javascript - $.ajax 获取数据不附加到 var

javascript - 无需重新加载即可返回上一页

javascript - Jasmine 测试 AngularJS $on

带有倒数计时器的 Javascript Cookie 超时

javascript - 无法调试 Node 子进程(使用 Node native 调试器)

javascript - 在这种情况下,是否有替代全局变量的良好做法?

javascript - Rails Guides : Getting Started 5. 13 删除文章:不会出现确认对话框

javascript - 如何使用 Jasmine 监视匿名函数