javascript - Sorting an array of objects based on a property in an Angular controller using $filter -


i have array of objects called $scope.segments this:

[   {     "_id": "55d1167655745c8d3679cdb5",     "job_id": "55d0a6feab0332116d74b253",     "status": "available",     "sequence": 1,     "body_original": "such fork",     "__v": 0,     "body_translated": "tal bifurcación"   },   {     "_id": "55d1167655745c8d3679cdb4",     "job_id": "55d0a6feab0332116d74b253",     "status": "available",     "sequence": 0,     "body_original": "so it.",     "__v": 0,     "body_translated": "así que esto es."   } ] 

i need order array sequence. so, need sequence 0 appear first, sequence 1 appear next, , on. in view, i'm doing , works:

<ul ng-repeat="segment in segments | orderby: 'sequence'">     <li>{{ segment.sequence }}</li> </u> 

however, need orderby filter work in controller. i'm doing this:

$scope.test = $filter('orderby')($scope.segments, 'sequence'); 

where, $scope.test should ordered array of objects based on the sequence property. however, when console.log('$scope.test') empty array ([]) shows. imagine $filter not working in controller.

any ideas on how solve this? thanks!

i guess didn't inject $filter service in controller properly, because worked me.

<body ng-controller="mainctrl">    <ul ng-repeat="segment in segments | orderby: 'sequence'">     <li>{{segment.sequence}}</li>   </ul>    <script src="vendors/angular.min.js"></script>    <script>     angular.module('app', [])      .controller('mainctrl', ['$scope', '$filter', function($scope, $filter) {       $scope.segments = [       {         "_id": "55d1167655745c8d3679cdb5",         "job_id": "55d0a6feab0332116d74b253",         "status": "available",         "sequence": 1,         "body_original": "such fork",         "__v": 0,         "body_translated": "tal bifurcación"       },       {         "_id": "55d1167655745c8d3679cdb4",         "job_id": "55d0a6feab0332116d74b253",         "status": "available",         "sequence": 0,         "body_original": "so it.",         "__v": 0,         "body_translated": "así que esto es."       }     ];       $scope.test = $filter('orderby')($scope.segments, 'sequence');       console.log($scope.test);     }]);   </script>  </body> 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -