Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS programatically call filter from service (sort by custom filter)

I have a the following situation (a translation filter in a service, used in the HTML file)

// serviceFile
angular.module('myModule')
  .service('translation')
  .filter('translate', function(translation) {
    // translate stuff
    return 'translatedString';
  });

// controllerFile
angular.module('myModule')
  .controller('StringsController', function(blabla, translation) {
    $scope.mySort = function() {
      return "some magic should happen here";
    };
  });

// htmlFile
<tr ng-repeat="string in strings">
  <td> 
      {{ string | translate: 'name' }} 
  </td>
</tr>

(The above code works, but perhaps some vital parts were omitted due to my lack of experience)

My issue is that I am requested to sort based on the translated values (something like string in strings | orderBy: mySearch) and I can't find how to call the filter programatically from the StringsController.mySearch

P.S. the filter is not returned from the service (don't know if this is relevant)

like image 542
norb Avatar asked Jun 02 '15 08:06

norb


1 Answers

You can see guide for filters

So in your case depends from defininition .filter('translate', you can use it like

.controller('StringsController', function(blabla, $filter) {
    //simple transtale
    var translatedString = $filter('translate')(stringForTranslate);

    //ordering
    var ordered = $filter('orderBy')(arrayForOrdering,function(el){ return  $filter('translate')(el); })

});
like image 197
Grundy Avatar answered Nov 01 '22 16:11

Grundy