【问题标题】:Angular js ng-repeat and filterAngular js ng-repeat和过滤器
【发布时间】:2016-11-21 08:54:03
【问题描述】:

我有一个嵌套的ng-repeat。我想通过动态选项来排列内部列表的顺序。

sort_option 值来自动态获取的 $scope 变量。这将使ul 列表中的所有内容都按排序顺序排列,但我只想对循环内的一个列表进行排序。

<ul>
<li ng-repeat='itemsli in mylists.lists'>
 <ul>
 <li ng-repeat="item in list | orderBy: sort_option | filter: {due_date_time : current_date_time}">
  {{item.name}}
 </li>
</ul>
</li>
</ul>

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    这样改行,在过滤器末尾添加: true

     <li ng-repeat="item in list | orderBy: sort_option | filter: {due_date_time : current_date_time}: true ">
    

    【讨论】:

      【解决方案2】:

      我不确定我是否理解正确

      但我像你一样创建了sample code,看起来效果很好

      列表是

      $scope.firstLists = [{id: 2},{id: 1},{id: 3}];
      $scope.secondLists = [{id: 2},{id: 1},{id: 3}];
      

      <ul>
      <li ng-repeat='itemsli in firstLists'>
        <ul>
           <li ng-repeat="item in secondLists | orderBy: 'id'">
           {{item.id}} of {{itemsli.id}}
           </li>
        </ul>
      </li>
      </ul>
      

      结果是

      1 of 2
      2 of 2
      3 of 2
      1 of 1
      2 of 1
      3 of 1
      1 of 3
      2 of 3
      3 of 3
      

      【讨论】:

        【解决方案3】:

        你在寻找这种嵌套的 ng-repeat 吗?

        var app = angular.module('soApp', []);
        app.controller('soController', function ($scope) {
            $scope.firstName = "John";
            $scope.lastName = "Doe";
            $scope.sortby_cards = function (sort_value) {
                console.log(sort_value);
                $scope.sort_option = sort_value;
            };
            $scope.firstLists = [{name: "def", vote: 2}, {name: "ret", vote: 1}, {name: "ghj", vote: 3}];
            $scope.secondLists = [{name: "pqr", vote: 2, attachment_count: 1},
                {name: "abc", vote: 1, attachment_count: 3},
                {name: "xyz", vote: 3, attachment_count: 2}];
        });
        

        【讨论】:

          【解决方案4】:

          我找到了一个基于Farzad Salimi Jazi's answer的解决方案:

          <ul>
          <li ng-repeat='lists in mylists.lists'>
          <md-button ng-click="sortby_cards('vote', lists)">Sort by votes</md-button>
          <md-button ng-click="sortby_cards('attachment_count', lists)">Sort by Attachments
          </md-button>
          
           <ul>
           <li ng-repeat="item in list | orderBy: list.sort_option | filter: {due_date_time : current_date_time}">
            {{item.name}}
           </li>
          </ul>
          </li>
          </ul>
          

          在控制器中:

          $scope.sortby_cards = function(sort_value,list){
              $scope.sort_option = sort_value;
              $scope.sort_index_val = index_val;
              list.sort_option = sort_value;
            }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-07-23
            • 1970-01-01
            • 2018-03-06
            • 1970-01-01
            相关资源
            最近更新 更多