2017-05-29 34 views
0

我發現數組排序不能保證。因此,我添加了一個動態數字並將這些對象嵌套在數組中,以便能夠使用編號來按順序排列項目。當我console.log我得到的項目正確的順序,但在視圖中時,他們出現在另一個順序。意外的ng-repeat數組結果

我有以下觀點:

<div id="objectives-report-single" ng-repeat="(key,value) in widgetItems"> 
     <tr ng-repeat="(keyInner,valueInner) in value"> 
      <td class="accountName">{{keyInner}}</td> 
      <td class="accountValue">{{valueInner}}</td> 
     </tr> 
    </table> 
</div> 

以下JS:`

   var n = 0; 
       var dynamicObject; 
       scope.widgetItems = {}; 
       angular.forEach(scope.report.executeResult.rowData, function (i) { 

       var a = scope.report.executeResult.rowData[n].values[0].value; 
       var name = 'a'; 

       dynamicObject = { 
        [eval(name)]: scope.report.executeResult.rowData[n].values[1].value 
       }; 

       scope.widgetItems[n] = dynamicObject; 
       n++; 
      });` 

console.logactual result

+1

數組排序*爲*保證。 *對象*排序不是。 – jonrsharpe

+0

是的,我試圖排列陣列中的對象 - >沒有成功,所以混合兩個 –

回答

0

解決。我將它們添加到已編號的對象數組中,然後在內部重複中循環。因此,我使用數組排序的原則,但JS中的對象不是。

<table class="table"> 
     <tr ng-repeat="(keyInner,valueInner) in value"> 
      <td class="accountName">{{keyInner}}</td> 
      <td class="accountValue">{{valueInner}}</td> 
     </tr> 
    </table> 

</div> 

      var n = 0; 
      scope.dynamicObject = {}; 
      scope.widgetItems = []; 
      angular.forEach(scope.report.executeResult.rowData, function (i) { 

       var a = scope.report.executeResult.rowData[n].values[0].value; 
       var name = 'a'; 

       scope.dynamicObject = { 
        [eval(name)]: scope.report.executeResult.rowData[n].values[1].value 
       }; 

       scope.widgetItems[n] = scope.dynamicObject; 
       n++; 


      });