0

I have a simple program to orderBy the lists by "age". But its not working and giving error! Pls help.

<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
  • what error you are getting Commented Apr 26, 2016 at 7:38

1 Answer 1

3

You have an error in your code. You need to use orderBy:'-age'"

Change your code to this and it works

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

WORKING DEMO BELOW

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.