如何在角度js中将数组从较低值循环到较高值?

时间:2014-12-12 07:26:00

标签: angularjs angularjs-directive

我是角度js的新手,我想在li tag中循环一个从较低值到较高值的数组。我在ng-init指令中声明了一个带有值的数组。我试图使用orderBy,但它不起作用。如何完成它?

<html>
  <head>
     <title>My Page</title>
  </head>

 <body ng-app="">
   <div ng-init="marks=[20,10,15,30,18,21,35]">
     <ul>
        <li ng-repeat="m in marks | orderBy:'marks'">{{m}}</li>
     </ul>
   </div>
   <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
 </body>
</html>

2 个答案:

答案 0 :(得分:1)

要按数组元素本身排序,而不是按其属性排序,可以使用toString()按字符串值排序,如果它们是数值,则使用valueOf()

<li ng-repeat="m in marks | orderBy:'valueOf()'">{{m}}</li>

http://jsfiddle.net/3yynLkyh/3/

要按降序排序,您可以在函数调用之前附加-

<li ng-repeat="m in marks | orderBy:'-valueOf()'">{{m}}</li>

答案 1 :(得分:0)

<html>
  <head>
     <title>My Page</title>
  </head>

 <body ng-app="">
   <div ng-init="marks=[20,10,15,30,18,21,35]">
     <ul>
        <li ng-repeat="m in marks | orderBy:'+'">{{m}}</li>
     </ul>
   </div>
   <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
 </body>
</html>

更改

  

ng-repeat =&#34; m in mark | ORDERBY:&#39; +&#39;&#34; 访问

     

ng-repeat =&#34; m in mark | ORDERBY:&#39; - &#39;&#34; FOR DECESSDING