Show only open div with angular if

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

I'm trying to acheive the same behavior as the spring code below:

<c:forEach items="${data}" var="data" varStatus="contador">     <c:if test="${(contador.count-1)%3==0}">         <div class="conjunto-${conjunto} row"> <!-- Show opening div -->     </c:if>          <!-- Some HTML goes here -->       <c:if test="${((contador.count-1)%3==2)}">          </div>     </c:if> </c:forEach> 

Explaining: I want a new div from class row only after 3 other HTML elements have been added. I have tried this with ng-if, like this:

<div ng-repeat="data in DATA">     <div class="conjunto-{{$index/3}} row" ng-show="$index % 3 == 0" ng-include="'html.html'">     </div>      <div ng-show="$index % 3 != 0" ng-include="'html.html'">     </div> </div> 

But it obviously doesnt work because only one element with be inside de div.row.

Is there an if-clause with which I could add only the opening div and then close it later?

Thanks in advance.

回答1:

Ok the best way to do this IMO is 2 fold, in your controller have a method similar to this

$scope.createDataChunks = function() {     var a = $scope.DATA,         retArr = [];     while(a.length) {        retArr.push(a.splice(0,3));     }      return retArr; } 

This would give you an array of arrays, each containing 3 (or less) items in it, you then have this as your markup

<div ng-repeat="data in createDataChunks()">     <div class="conjunto-{{$index/3}} row" ng-show="$index % 3 == 0" ng-include="'html.html'">         <!-- put custom HTML here -->     </div> </div> 

I think that's roughly what you are after?



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!