AngularJS以编程方式从服务调用过滤器(按自定义过滤器排序)

我有以下情况(服务中的翻译过滤器,用于HTML文件中)

// 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>

你可以看到过滤器的指南

所以你的情况取决于defininition .filter('translate',你可以使用它

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

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

});
链接地址: http://www.djcxy.com/p/26851.html

上一篇: AngularJS programatically call filter from service (sort by custom filter)

下一篇: Determine if mouse outside window