admin管理员组

文章数量:1313120

I am trying to order an array of objects by using a parator function, but it seems the parator function is pletely ignored (See the angular documentation).

I am using angularJS 1.5.6.

Here is a JSFiddle

Html:

<script src=".js/1.5.6/angular.min.js">
</script>
<body ng-app="app" ng-controller="ctrl">
    {{msg}}
</body>

JavaScript:

angular.module("app", [])

.factory('f1', function($filter) { 

    var f1 = {};

    function parator(a,b) { console.log(a,b); return a.id - b.id; }
    function getter(x) { /*console.log(x);*/ return x; }

    f1.testOrderBy = function() {
        return $filter('orderBy')(
            [ {id:3}, {id:1}, {id:2} ], 
            getter, 
            false, 
            parator
        )
        .map(function(x) { return x.id; })
    };

    return f1;
})

.controller("ctrl", function($scope, f1) {
    $scope.msg = f1.testOrderBy();
})

My question: Why is parator ignored? (This can be seen since the console.log() call is never made). Is this an angularJS bug?

Because of this I cannot even order an array of objects using a custom parator.

Thanks!

I am trying to order an array of objects by using a parator function, but it seems the parator function is pletely ignored (See the angular documentation).

I am using angularJS 1.5.6.

Here is a JSFiddle

Html:

<script src="https://cdnjs.cloudflare./ajax/libs/angular.js/1.5.6/angular.min.js">
</script>
<body ng-app="app" ng-controller="ctrl">
    {{msg}}
</body>

JavaScript:

angular.module("app", [])

.factory('f1', function($filter) { 

    var f1 = {};

    function parator(a,b) { console.log(a,b); return a.id - b.id; }
    function getter(x) { /*console.log(x);*/ return x; }

    f1.testOrderBy = function() {
        return $filter('orderBy')(
            [ {id:3}, {id:1}, {id:2} ], 
            getter, 
            false, 
            parator
        )
        .map(function(x) { return x.id; })
    };

    return f1;
})

.controller("ctrl", function($scope, f1) {
    $scope.msg = f1.testOrderBy();
})

My question: Why is parator ignored? (This can be seen since the console.log() call is never made). Is this an angularJS bug?

Because of this I cannot even order an array of objects using a custom parator.

Thanks!

Share Improve this question asked Jun 17, 2016 at 9:07 FlorisFloris 1,12312 silver badges27 bronze badges 2
  • 1 surprisingly the plunker example they gave on site is also not working as they explained in the documentation plnkr.co/edit/FSq6AwlpdCRmTEQ7hefL?p=preview – Deep Commented Jun 17, 2016 at 9:29
  • Great catch Deep! Didn't even notice that! – Floris Commented Jun 17, 2016 at 9:48
Add a ment  | 

1 Answer 1

Reset to default 13

Because the support for custom parators in orderBy was added in 1.5.7.

You can read the changelog here.

If you check the documentation for 1.5.6 you will see that the api is described as:

$filter('orderBy')(array, expression, reverse)

While in 1.5.7 it is:

$filter('orderBy')(collection, expression, reverse, parator)

本文标签: javascriptWhy does AngularJS orderBy filter ignore the comparator parameterStack Overflow