【问题标题】:Using ng-repeat index to display different html使用 ng-repeat 索引显示不同的 html
【发布时间】:2013-11-17 20:30:12
【问题描述】:

我正在使用 Angular 和 ng-repeat 以及 Semantic UI Grid 布局。

我正在尝试连续显示 5 列。当达到第五个项目时,创建一个新行。下面为数据中的每个项目创建一个新的行和列。我可以看到有一个带有 ng-repeat 的索引属性,但不确定如何基本上说 if mod 5 == 0 输出一个新行元素,否则只输出一个新列。

<div class="ui grid">
    <div class="row" ng-repeat="item in data.results">
        <div class="column">
            <div class="ui segment">
                {{item.original_title}}
            </div>
        </div>
    </div>
</div>

感谢您的帮助

【问题讨论】:

  • 在我看来,最简洁的方法是为此编写一个小的新指令。
  • 那会怎样;)

标签: angularjs angularjs-ng-repeat semantic-ui


【解决方案1】:

在我看来,最简洁和最易于维护的方式是编写指令。在您的 HTML 中,您只需:

<grid source="data.results" break="5" />

在您的 JS 中,您将数据分解为控制器内的正确结构并使用模板输出:

    angular.module('yourApp', [])
    .directive('grid', function() {
      return {
          restrict: 'E',
          scope: {
            break: '=break',
            source: '=source'
          },
          controller: function($scope) {
            var total = Math.ceil($scope.source.length / $scope.break);
            $scope.data = new Array(total);
            for (var i = 0; i < total; ++i) {
              $scope.data[i] = $scope.source.slice(i * $scope.break, (i + 1) * $scope.break);
            }
          },
          template:
            '<div class="ui grid">' +
            '  <div class="row" ng-repeat="row in data">' +
            '    <div class="column" ng-repeat="item in row">' +
            '      <div class="ui segment">' +
            '        {{item.original_title}}' +
            '      </div>' +
            '    </div>' +
            '  </div>' +
            '</div>',
          replace: true
      };
  });

【讨论】:

    【解决方案2】:

    这是一种使用范围过滤器来解决此问题的方法。

    我们计算您需要多少行 (data.results.length/5),然后使用带有范围过滤器的 ng-repeat 迭代该行数。

    然后在每一行中,我们 slice 取出该行所需的列,并使用另一个 ng-repeat 创建这些列。

    <div class="row" ng-repeat="n in [] | range:(data.results.length/5)+1">
        <span ng-repeat='item in data.results.slice($index*5,($index*5+5))'>
            <div class="column">
               <div class="ui segment">
                  {{item.original_title}}
               </div>
            </div>
        </span>
    </div>
    

    以及相关的范围过滤器(来自:http://www.yearofmoo.com/2012/10/more-angularjs-magic-to-supercharge-your-webapp.html#more-about-loops):

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

    这是一个小提琴:http://jsfiddle.net/BMrNs/1/

    【讨论】:

      【解决方案3】:

      尝试将ng-switchArray.slice() 结合使用,这不是最有效的方法,但如果您没有任何办法干预数据表示,这是我能想到的最好方法。

      <div class="ui grid">
          <div ng-repeat="item in data.results">
              <div ng-switch on="$index % 5">
                  <div ng-switch-when="0" class="row">
                      <div ng-repeat="e in data.results.slice($index, $index + 5)">
                            <div class="column">
                                <div class="ui segment">
                                    {{e.original_title}}
                                </div>
                            </div>
                      </div>
                  </div>
              </div>
          </div>
      </div>
      

      【讨论】:

      • 行标记只是一个带有“行”类的 div,正如您在问题中看到的 ng-repeat 所在的位置,所以不确定我会循环播放什么
      • 抱歉误解了这个问题。我会编辑我的答案。这个想法仍然存在。
      • 列 div 然后进入行 div,如果这有意义的话,那么一行 5 列,然后是一个带有列的新行等
      猜你喜欢
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 2014-06-11
      相关资源
      最近更新 更多