2015-10-27 36 views
1

您好,我從顯示頻道信息的YouTube API中提取數據。下面是Ang-ng-repeat orderBy not working

channels: { 
    UCxxxxxxxxxxx: { 
     channelId: "xxxx", 
     channelTitle: "xxxx", 
     description: "xxxx", 
     channelId: "xxxx", 
     title: "xxxx" 
    }, 
    UCxxxxxxxxxxx: { 
     channelId: "xxxx", 
     channelTitle: "xxxx", 
     description: "xxxx", 
     channelId: "xxxx", 
     title: "xxxx" 
    } 
} 

我已通過channels對象$scope.results

我剛複製的例子從角文檔像這樣在控制器內部

$scope.predicate = 'title'; 
    $scope.reverse = true; 
    $scope.order = function(predicate) { 
     $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false; 
     $scope.predicate = predicate; 
    }; 

這裏是JSON的一個例子html標記

 <table> 
      <thead> 
       <tr> 
        <a href="" ng-click="order('title')">Name</a> 
        <span class="sortorder" ng-show="predicate === 'title'" ng-class="{reverse:reverse}"></span> 
       </tr> 
       <tr> 
        <a href="" ng-click="order('channelTitle')">Channel</a> 
        <span class="sortorder" ng-show="predicate === 'channelTitle'" ng-class="{reverse:reverse}"></span> 
       </tr> 
       <tr> 
        <a href="" ng-click="order('viewCount')">Views</a> 
        <span class="sortorder" ng-show="predicate === 'viewCount'" ng-class="{reverse:reverse}"></span> 
       </tr> 
       <tr> 
        <a href="" ng-click="order('subscriberCount')">Subscribers</a> 
        <span class="sortorder" ng-show="predicate === 'subscriberCount'" ng-class="{reverse:reverse}"></span> 
       </tr> 
      </thead> 
      <tbody> 
       <tr ng-repeat="result in results | orderBy:predicate:reverse"> 
        <td ng-bind="result.title"></td> 
        <td> 
         <a ng-show="result.channelTitle" href="https://www.youtube.com/channel/{{result.channelId}}" ng-bind="result.channelTitle"></a> 
         <div ng-show="!result.channelTitle" ng-bind="'--'"><div> 
        </td> 
        <td ng-bind="result.info.viewCount | number"></td> 
        <td ng-bind="result.info.subscriberCount | number"></td> 
       </tr> 
      </tbody> 
     </table> 

當我點擊表頭,沒有錯誤。所以我不能確切地弄清楚什麼不起作用。任何幫助表示讚賞。謝謝。

回答

2

您的對象(用於ng-repeat)與原始示例(https://docs.angularjs.org/api/ng/filter/orderBy)不同。

channels: { 
    UCxxxxxxxxxxx: { 
     channelId: "xxxx", 
     channelTitle: "xxxx", 
     description: "xxxx", 
     channelId: "xxxx", 
     title: "xxxx" 
    }, 
    UCxxxxxxxxxxx: { 
     channelId: "xxxx", 
     channelTitle: "xxxx", 
     description: "xxxx", 
     channelId: "xxxx", 
     title: "xxxx" 
    } 
} 

結果必須是數組。 原始數據:

$scope.friends = 
      [{name:'John', phone:'555-1212', age:10}, 
      {name:'Mary', phone:'555-9876', age:19}, 
      {name:'Mike', phone:'555-4321', age:21}, 
      {name:'Adam', phone:'555-5678', age:35}, 
      {name:'Julie', phone:'555-8765', age:29}];