为什么orderBy不在angularjs的ngrepeat工作?

时间:2016-04-30 05:17:16

标签: javascript angularjs angularjs-directive angularjs-scope

我正在使用角度j在orderBy中使用ng-repeat但是它无法对我的列表进行排序。我的代码是什么?

如何对我的代码进行排序

https://jsfiddle.net/7MhLd/1921/

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
    $scope.lines = {
        "a" : {name:"bb"},
        "aa": {name: 'aa'},
        "zz": {name:"zz"},
        "oo": {name: 'oo'}, 
        "kk": {name:"k"},
        "j" : {name: "a"},
        "n" : {name:"n"},
        "c" : {name: "c"}
   }
} 

1 个答案:

答案 0 :(得分:0)

您必须将对象转换为数组以使其正常工作

<div ng-controller="MyCtrl">
  <div ng-repeat="line in lines | orderBy:'name'">
    <div class="preview">{{line.name}} {{$index}}</div>
  </div>
</div>

和控制器

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
    $scope.lines = [
       {name:"bb"},
    {name: 'aa'},
    {name:"zz"},
    {name: 'oo'}, 
    {name:"k"},
    {name: "a"},
    {name:"n"},
    {name: "c"}
];
}