2016-01-20 25 views
0

訂購ng-repeat中的項目後有些問題。用錯誤的索引點擊項目打開頁面後。 我的示例HTML代碼,打開的頁面中正確的是:

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

如果添加排序依據,顯示錯誤後指數:

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

而我的$範圍功能showPost():

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

回答

0

我認爲問題在於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'); 
    };