AngularJS programatically call filter from service (sort by custom filter)
我有以下情况(服务中的翻译过滤器,用于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>
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); })
});
链接地址: http://www.djcxy.com/p/26852.html