AngularJS ng-repeat创建div

时间:2014-05-13 12:00:19

标签: angularjs angularjs-ng-repeat

我想使用AngularJs创建一些div结构,但它根本不起作用。 我该如何使用范围?

<div ng-controller = "gameController">
    <div class="table">
        <div ng-repeat = "i in [] | range:game.nrLines">
            <div class="trow">
                <div ng-repeat = "j in [] | range:game.nrCols">
                    <span class="element">
                        <img src="img/elep.png" class="element" data-col = {{j}} data-line= {{i}}>
                    </span>
                </div>
            </div>
        </div>
    </div>
</div>

这是Angular代码:

var gameApp = angular.module("gameApp", []);
gameApp.filter('range', function() {
    return function(input) {
        for (var i=0; i<input; i++) {
            input.push(i);          
        }
        return input;
    }
});

gameApp.controller("gameController", function($scope) {
    $scope.game = new Game();
    $scope.nrCols = $scope.game.matrix.getNrCols;
    $scope.nrLines = $scope.game.matrix.getNrLines;
});

谢谢!

1 个答案:

答案 0 :(得分:3)

您的过滤器缺少该范围的参数。输入是您将从过滤器返回的内容,因此您只需要为实际计数添加另一个参数:

gameApp.filter('range', function() {
    return function(input, count) {
        for (var i=0; i<count; i++)
            input.push(i);          
        return input;
    }
});

我做了一个plnkr here来帮助