单击ng-repeat元素更新数组模型

时间:2016-05-04 14:35:29

标签: arrays angularjs angularjs-ng-repeat angular-ngmodel angularjs-ng-click

我有一个Cell对象的二维数组,这个数组在我的html中由一些ng-repeat <td>元素表示,我想在<td>时更新我的​​单元格点击。我不知道如何将<td>绑定到数组中的相应单元格 这是我试过的,它没有给我任何错误,数组显示在html但ng-click事件不起作用:

HTML:

<div ng-app = "game" ng-controller = "gameCtrl" id = "gameLayout">
    <div id = "boardLayout">
        <table id = "grid">
            <tr ng-repeat = "y in grid">
                <td ng-repeat = "x in y" ng-model = "grid[y][x]" ng-click = "grid[y][x].tryPlay(game)"></td>
            </tr>
        </table>
    </div>
</div>


使用Javascript:

Cell.prototype.tryPlay = function(game)
{
    console.log("tryPlay call.");
}
// ...
var game = angular.module("game", []);
game.controller("gameCtrl", function($scope, $interval)
{
    var grid = [[new Cell(), new Cell(), new Cell(), new Cell(), new Cell()],
                       [new Cell(), new Cell(), new Cell(), new Cell(), new Cell()],
                       [new Cell(), new Cell(), new Cell(), new Cell(), new Cell()],
                       [new Cell(), new Cell(), new Cell(), new Cell(), new Cell()],
                       [new Cell(), new Cell(), new Cell(), new Cell(), new Cell()]];

    var game = new Game(grid);

    $scope.game = game;
    $scope.grid = game.grid;
});

2 个答案:

答案 0 :(得分:2)

    function Cell(){
      return this;
    }
    Cell.prototype.tryPlay = function(game)
    {
        console.log("tryPlay call.");
        this.value="CLIKED";
    }
    // ...
    var game = angular.module("game", []);
    game.controller("gameCtrl", function($scope, $interval)
    {
        var grid = [[new Cell(), new Cell(), new Cell(), new Cell(), new Cell()],
                           [new Cell(), new Cell(), new Cell(), new Cell(), new Cell()],
                           [new Cell(), new Cell(), new Cell(), new Cell(), new Cell()],
                           [new Cell(), new Cell(), new Cell(), new Cell(), new Cell()],
                           [new Cell(), new Cell(), new Cell(), new Cell(), new Cell()]];

        //var game = new Game(grid);

       // $scope.game = game;
        $scope.game="";
        $scope.grid = grid;
    });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app = "game" ng-controller = "gameCtrl" id = "gameLayout">
        <div id = "boardLayout">
            <table id = "grid">
                <tr ng-repeat = "y in grid">
                    <td ng-repeat = "x in y" ng-click = "x.tryPlay(game)">CLICK ME <span style="color:red" ng-bind="x.value"></span></td>
                </tr>
            </table>
        </div>
    </div>

我在TD中添加了一个文本,否则它们没有宽度,所以我无法点击它们。

x和y不是索引,它们是对象。所以y是一个单元格数组,x是一个单元格实例。所以你在表情中直接使用它们就会发生魔力。

答案 1 :(得分:1)

我认为您的问题出在您的ngModel和ngClick参数中。 尝试以这种方式使用它们:

 <tr ng-repeat = "y in grid">
     <td ng-repeat = "x in y" ng-model = "x" ng-click = "x.tryPlay(game)"></td>
 </tr>

如果我理解你正在尝试做什么。这应该工作。 基本上当你在网格中重复y时。 Y绑定到重新发送行的不同数组。当您在此转发器中使用x in y时,x将绑定到数组内的单元对象。您应该只能使用x。

来使用单元格对象功能