简单的orderBy过滤器在AngularJs中不起作用

时间:2016-04-26 07:37:34

标签: javascript 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>

1 个答案:

答案 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>