Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
34
rated 0 times [  37] [ 3]  / answers: 1 / hits: 28362  / 11 Years ago, thu, march 14, 2013, 12:00:00

I would like to know if there is a way to inject the filters in a service in AngularJs.



I've been trying



app.factory('educationService', [function($rootScope, $filter) {

// ..... Some code

// What I want
console.log(dateFilter(new Date(), 'yyyy-MM-01'));

// ..... Some code

}]);


So I would like to know if it is possible to inject a filter in a service or maybe it is accessible in another way.



And if you have a link to a documentation about this point, it would be really nice :) Been searching in the doc of Angular and I found nothing really helpful about this.



Thanks :)


More From » angularjs

 Answers
30

First you have to inject the filter like this:



app.factory('myService', ['$rootScope', '$filter', function($rootScope, $filter) 


(The array is only needed when you use minification in your build process)



To call specific filter programmatically:



$filter('date')(new Date(), 'yyyy-MM-01');


$filter(name)returns the specific filter function, which you can than call with your arguments:



var dateFilter = $filter('date');
var filteredDate = dateFilter(new Date(), 'yyyy-MM-01')

[#79600] Wednesday, March 13, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reecep

Total Points: 141
Total Questions: 95
Total Answers: 113

Location: Finland
Member since Mon, Nov 8, 2021
3 Years ago
;