Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

orderBy not working as expected: Angularjs

My array is : BS. Its structure is :

Array[317]
0: Object
   $$hashKey: "022"
   name: "Los Angeles Comm."
.
.
.
..

BS is an array. Each value is a JSon object with filed of name.

I want to sort all the values of BS according to their name. I am trying :

<option ng-repeat="item in BS | orderBy:item.name"  value="{{item.name}}">{{item.name}}</option>

I have also tried : orderBy:name and orderBy:item[name]. Nothing works. Why is this not working and whats the correct code?

like image 563
Bhumi Singhal Avatar asked May 09 '13 06:05

Bhumi Singhal


1 Answers

Have a look at below html

<!DOCTYPE html>
<html ng-app="app">
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
</head>
<body>
    <div ng-controller="item">
        <ul>
            <li ng-repeat="item in items|orderBy:'name'">
                {{item.name}}
            </li>
        </ul>
    </div>
    <script>
        var AppModule = angular.module('app', []);
        function item($scope) {
            $scope.items = [{ name: 'tur' }, { name: 'abc' }, { name: 'xyx' }];

        }
    </script>
</body>
</html>
like image 165
Ajay Beniwal Avatar answered Nov 06 '22 02:11

Ajay Beniwal