【问题标题】:How to add content inside div with angular without using jquery如何在不使用jquery的情况下使用角度在div中添加内容
【发布时间】:2017-03-18 21:07:24
【问题描述】:

我正在尝试以角度 1 构建一个基本的井字游戏。当我点击一个正方形时,我想根据转弯添加 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

【问题讨论】:

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


    【解决方案1】:

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

    当您单击一个正方形时,将其传递给您的 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 自动函数执行的意义何在?
    • 查看 AngularJS 团队认可的 Angular 风格指南的 Javascript Scopes y010 部分 :)
    • 知道了。另外,如果你不传递任何东西,为什么要使用 .$inject 呢?
    • 好的,知道了。最后一个问题......我怎样才能设计出这个样式,让它看起来像一个井字游戏?这很棘手,因为它在 ng-repeat 中
    • 我们不应该在 cmets 中讨论更多内容,但作为提示,使用带有 ngClass 指令的 CSS 类,使用 $index/square.id 上的模运算符来获得您的 3 .或者使用 ngIfngShow 指令和模数运算符来添加一个 div 来包含一行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 2011-06-30
    • 2021-08-01
    • 1970-01-01
    相关资源
    最近更新 更多