2017-01-27 30 views
1

我正在尋找一種方法讓一個ng-重複該過濾器或「訂單」或「id」。 因此,像這樣:具有兩個表達式的ng-repeat條件過濾器

ng-repeat "cd in cds | filter: !order ? orderBy:'id' : orderBy:'order'" 

所以,如果訂單是空的(沒有數據),它應該由ID順序,如果訂單確實有它應該通過「訂單」訂單數據。這可能嗎?它現在給出了兩倍的相同數據輸出,條件似乎不起作用

+0

http://stackoverflow.com/questions/22621425/angularjs-filter-expression-in-ng-repeat – Merlin

+0

更簡單的解決方案發布 –

回答

1

你靠得很近;嘗試是這樣的:

ng-repeat "cd in cds | orderBy: (!order ? 'id' : 'order')" 
+0

確實有趣的解決方案,謝謝。某種程度上沒有得到!命令的工作必須在控制器中創建一個$ scope。在答案中告訴你我是如何運作的 –

1

謝謝安德魯鑽石,這成爲了NG-重複我

ng-repeat="cd in cds| orderBy: (ordering != null ? 'order' : 'id')" 

這個控制器(不得不添加$ scope.ordering):

function GetCDS() { 
     $http({ 
      method: 'Get', 
      url: "/cds" 
     }) 
      .success(function (data, status, headers, config) {   
       $scope.cds = data; 
       $scope.ordering = data[0].order; 
      }) 
      .error(function (data, status, headers, config) { 
       $scope.message = 'Unexpected Error'; 
      }); 
    }