ng-repeat中的angular.js条件标记

ebo*_*ard 13 twitter-bootstrap angularjs angular-ui

我正在使用angular.js和(为了参数)bootstrap.现在我需要迭代"事物"并在"行"中显示它们:

<div class="row">
  <div class="span4">...</div>
  <div class="span4">...</div>
  <div class="span4">...</div>
</div>
<div class="row">
  etc...
Run Code Online (Sandbox Code Playgroud)

现在,我怎么能用.row角度来关闭每三分之一的div?我试过ui-if来自angular-ui,但即便如此也没有.

如果我要使用服务器端渲染,我会做这样的事情(JSP语法在这里,但无关紧要):

<div class="row>
  <c:forEach items="${things}" var="thing" varStatus="i">
    <div class="span4">
        ..
    </div>
  <%-- Here is the trick:--%>
  <c:if test="${i.index % 3 == 2}">
          </div><div class="row">
  </c:if>
  </c:forEach>
</div>
Run Code Online (Sandbox Code Playgroud)

请注意,我需要在这里实际更改DOM,而不仅仅是css隐藏元素.我尝试重复.row.span4divs,没有用.

Mat*_*erg 19

编辑2013年11月12日

似乎1.2中的角度变化不仅有点变化,而且还有更好的方法.我创建了两个过滤器.我试图将它们合二为一,但却出现了摘要错误.以下是两个过滤器:

.filter("mySecondFilter", function(){
    return function(input, row, numColumns){
        var returnArray = [];
        for(var x = row * numColumns; x < row * numColumns + numColumns; x++){
            if(x < input.length){
                returnArray.push(input[x]);                    
            }
            else{
                returnArray.push(""); //this is used for the empty cells
            }
        }
        return returnArray;   
    }
})
.filter("myFilter", function(){
    return function(input, numColumns){
        var filtered = [];
        for(var x = 0; x < input.length; x++){
            if(x % numColumns === 0){
                filtered.push(filtered.length);
            }
        }
        return filtered;
    }
});
Run Code Online (Sandbox Code Playgroud)

现在html将如下所示:

<table border="1">
     <tr data-ng-repeat="rows in (objects | myFilter:numColumns)">
          <td data-ng-repeat="column in (objects | mySecondFilter:rows:numColumns)">{{ column.entry }}</td>
     </tr>  
</table>
Run Code Online (Sandbox Code Playgroud)

jsFiddle:http://jsfiddle.net/W39Q2/


编辑2013年9月20日

在处理需要动态列的大量数据时,我想出了一个更好的方法.

HTML:

<table border="1">
    <tr data-ng-repeat="object in (objects | myFilter:numColumns.length)">
        <td data-ng-repeat="column in numColumns">{{ objects[$parent.$index * numColumns.length + $index].entry }}</td>
    </tr>  
</table>
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

$scope.objects = [ ];
for(var x = 65; x < 91; x++){
    $scope.objects.push({
        entry: String.fromCharCode(x)
    });
}

$scope.numColumns = [];
$scope.numColumns.length = 3;
Run Code Online (Sandbox Code Playgroud)

新过滤器:

.filter("myFilter", function(){
    return function(input, columns){
        var filtered = [];
        for(var x = 0; x < input.length; x+= columns){
             filtered.push(input[x]);   
        }
        return filtered;
    }
});
Run Code Online (Sandbox Code Playgroud)

这使它成为动态的.要更改列,只需更改numColumns.length.在js小提琴中你可以看到我已将它连接到下拉列表.

jsFiddle:http://jsfiddle.net/j4MPK/


你的HTML标记看起来像这样:

<div data-ng-repeat="row in rows">
    <div data-ng-repeat="col in row.col">{{col}}</div>
</div>
Run Code Online (Sandbox Code Playgroud)

然后你可以在你的控制器中创建一个变量,如下所示:

$scope.rows = [
    {col: [ 1,2,3,4 ]},
    {col: [ 5,6,7 ]},
    {col: [ 9,10,11,12 ]}
]; 
Run Code Online (Sandbox Code Playgroud)

这样,您可以拥有任意数量的列.

jsfiddle http://jsfiddle.net/rtCP3/39/


编辑我已修改小提琴现在支持具有平面对象数组:

jsfiddle: http ://jsfiddle.net/rtCP3/41/

html现在看起来像这样:

<div class="row" data-ng-repeat="row in rows">
    <div class="col" data-ng-repeat="col in cols">
        {{objects[$parent.$index * numColumns + $index].entry}}
    </div>
</div>  
Run Code Online (Sandbox Code Playgroud)

然后在控制器中我有:

$scope.objects = [
    {entry: 'a'},
    {entry: 'b'},
    {entry: 'c'},
    {entry: 'd'},
    {entry: 'e'},
    {entry: 'f'},
    {entry: 'g'},
    {entry: 'h'}    
];

$scope.numColumns = 3;
$scope.rows = [];
$scope.rows.length = Math.ceil($scope.objects.length / $scope.numColumns);
$scope.cols = [];
$scope.cols.length = $scope.numColumns;
Run Code Online (Sandbox Code Playgroud)

$ scope.numColumns变量用于指定每行中需要的列数.


要处理动态数组大小更改,请将监视放在数组的长度上(而不是整个数组,这将是redundent)

$scope.numColumns = 3;  
$scope.rows = [];    
$scope.cols = [];    
$scope.$watch("objects.length", function(){
    $scope.rows.length = Math.ceil($scope.objects.length / $scope.numColumns);
    $scope.cols.length = $scope.numColumns;        
});
Run Code Online (Sandbox Code Playgroud)

jsfiddle:http://jsfiddle.net/rtCP3/45/


tim*_*der 15

为什么不使用像这样简单的东西? http://jsfiddle.net/everdimension/ECCL7/3/

<div ng-controller="MyCtrl as ctr">
    <div class="row" ng-repeat="project in ctr.projects" ng-if="$index % 3 == 0">
         <h4 class="col-sm-4" ng-repeat="project in ctr.projects.slice($index, $index+3)">
            {{project}}
        </h4>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)