Angular.js NG重复:开/ X次迭代之后关闭元素 [英] Angular.js ng-repeat: opening/closing elements after x iterations

查看:79
本文介绍了Angular.js NG重复:开/ X次迭代之后关闭元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的NG-重复回事:

I have a simple ng-repeat going on:

<div class="row-fluid">
  <div class="span4" ng-repeat="piece in clothes | filter:query">
    <img ng-src="{{piece.mainImage[0]}}" class="thumbImg" />
    <a href="#/json/{{piece.systemName}}">{{piece.name}}</a>
  </div>
</div>

在3次重复,我想停止重复,添加在结束&LT; D​​IV&GT; ,并打开一个新的 .row-液体(开始新线),然后重新开始,我不放过,插入标记每次第三次循环。

After 3 repeats, I'd like to stop the repeat, add in a closing <div> and open a new .row-fluid (to start a new line), then re-start the loop where I left off, inserting the tags each 3rd time.

有角的文档真的很难穿越,从而很难制定出如何做到这一点。

The docs for Angular are really hard to traverse, making it difficult to work out how to do this.

推荐答案

ngRepeat将继续重新评估无限的过滤器,因为它的每一个它的运行时间得到不同的值。

ngRepeat will keep re-evaluating the filter infinitely because it gets different values every time it's ran.

这可怕code似乎在一般情况下工作。如果你有相同的数组,事情会变得毛。我们需要能够比较数组相等,但是这是一个困难的问题。我把易/站不住脚的出路,只是用字符串化()。无法获得 angular.equals()来为我工作;我想这可能只是作用于浅的对象。

This terrible code seems to work in the general case. If you have identical arrays things could get hairy. We need to be able to compare arrays for equality, but that's a difficult problem. I took the easy/flimsy way out and just used stringify(). Could not get angular.equals() to work for me; I think it probably just works on shallow objects.

app.filter('partition', function($cacheFactory) {
  var arrayCache = $cacheFactory('partition')
  return function(arr, size) {
    var parts = [], cachedParts,
      jsonArr = JSON.stringify(arr);
    for (var i=0; i < arr.length; i += size) {
        parts.push(arr.slice(i, i + size));        
    }
    cachedParts = arrayCache.get(jsonArr); 
    if (JSON.stringify(cachedParts) === JSON.stringify(parts)) {
      return cachedParts;
    }
    arrayCache.put(jsonArr, parts);

    return parts;
  }; 
});

这篇关于Angular.js NG重复:开/ X次迭代之后关闭元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆