2016-04-26 98 views
0

我有一個簡單的程序來訂購按「年齡」列表。但它不工作,給錯誤!請幫助。簡單的orderBy過濾器不能在AngularJs中工作

<html> 
<head> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js" ></script> 
</head> 
<body ng-app="myApp" ng-controller="myCtrl"> 
See here: <p ng-repeat="x in persons | orderby: age">{{x.name}},{{x.age}}</p> 
<script> 
//module declaration 
var app = angular.module("myApp",[]); 
//controller declaration 
app.controller('myCtrl',function($scope){ 
    $scope.persons=[ 
     {name:"Peter",age:23},{name:"Lina",age:36},{name:"Robert",age:31} 
    ]; 
}); 
</script> 
</body> 
</html> 
+0

什麼樣的錯誤你得到 –

回答

3

您在代碼中存在錯誤。您需要使用orderBy:'-age'"

更改您的代碼這一點,它的工作原理

See here: <p ng-repeat="x in persons | orderBy:'-age'">{{x.name}},{{x.age}}</p> 

工作演示低於

<html> 
 
<head> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js" ></script> 
 
</head> 
 
<body ng-app="myApp" ng-controller="myCtrl"> 
 
See here: <p ng-repeat="x in persons | orderBy:'-age'">{{x.name}},{{x.age}}</p> 
 
<script> 
 
//module declaration 
 
var app = angular.module("myApp",[]); 
 
//controller declaration 
 
app.controller('myCtrl',function($scope){ 
 
    $scope.persons=[ 
 
     {name:"Peter",age:23},{name:"Lina",age:36},{name:"Robert",age:31} 
 
    ]; 
 
}); 
 
</script> 
 
</body> 
 
</html>