【问题标题】:Wrong $index after order by in Angular在Angular中订购后错误的$index
【发布时间】:2016-01-24 03:00:30
【问题描述】:

在 ng-repeat 中订购商品后遇到一些问题。单击索引错误的项目打开页面后。 我的示例 HTML 代码,打开页面正确:

<div ng-repeat="item in items" ng-click="showPost($index)">{{item.title}}</div>

如果添加orderby,显示错误的帖子索引:

<div ng-repeat="item in items | orderBy:'-title'" ng-click="showPost($index)">{{item.title}}</div>

还有我的 $scope 函数 showPost():

  $scope.showPost = function(index){
    $rootScope.postContent = $scope.catItems[index];
    $scope.ons.navigator.pushPage('post.html');
  };

【问题讨论】:

    标签: angularjs-ng-repeat angularjs-orderby


    【解决方案1】:

    我认为问题在于 ngRepeat 中数组的顺序(在应用 orderBy 过滤器之后)与 $scope.catItems 的顺序不同。

    最好通过将对象本身作为函数参数传递并在数组中查找对象而不是传递索引来修复代码。 假设 $scope.items 和 $scope.catItems 是不同的数组,试试这样:

    // HTML
    <div ng-repeat="item in items | orderBy:'-title'" ng-click="showPost(item)">{{item.title}}</div>
    // JS
    
    $scope.showPost = function(item){
        var index = $scope.items.indexOf(item);
        $rootScope.postContent = $scope.catItems[index];
        $scope.ons.navigator.pushPage('post.html');
      };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      • 1970-01-01
      • 2013-04-13
      • 2012-09-21
      • 1970-01-01
      相关资源
      最近更新 更多