Is it possible to extend or override existing filters?

1,749 views
Skip to first unread message

Konstantin Cherkasoff

unread,
Sep 2, 2012, 10:49:05 AM9/2/12
to ang...@googlegroups.com
Hi All!
Is it possible to extend existing "standard" filters (date, number, lowercase etc)?
In my case I need to parse date from 'YYYYMMDDhhmmss' format so I'd like to extend (or override) "date" filter instead of writing my own.

Pawel Kozlowski

unread,
Sep 2, 2012, 11:36:29 AM9/2/12
to ang...@googlegroups.com
Hi Konstantin!
Responded on SO:
http://stackoverflow.com/questions/12236905/how-to-extend-or-override-existing-filters-in-angularjs/12237229#12237229
although I'm not sure if I understood your question / use case
correctly. Feel free to respond more on this list / SO so we can work
it out and I will (eventually) change the answer on SO if needed.

Cheers,
Pawel

Konstantin Cherkasoff

unread,
Sep 2, 2012, 5:16:43 PM9/2/12
to ang...@googlegroups.com
Hi Pawel!myApp.filter('customDate', function($filter){    
    var standardDateFilterFn = $filter('date');
    return function(dateToFormat){
     return 'prefix ' + standardDateFilterFn(dateToFormat, 'yyyyMMddhhmmss');
    }    
});


Thank you for this example.
Actualy in my case it would be better for me to override date filter rather than extend.
Is it possible to define filter like below?
myApp.filter('date', function($filter){...

--
Konstantin Cherkasoff

Witold Szczerba

unread,
Sep 2, 2012, 6:57:13 PM9/2/12
to ang...@googlegroups.com
Hi,
I am not sure if it is possible to override filters - check it out.
However, I would suggest not doing so - even if that is allowed.
Predefined filters are the public API of AngularJS. What if some parts
of AngularJS use some of them internally or one day you install some
add-on which depends on that filter?

By the way: there is no need to inject $filter, you can specify which
exactly filters you want on injection point, so you are more specific
about real dependencies:
myApp.filter('myDate', function(dateFilter){
return function(date){
return 'prefix ' + dateFilter(date, 'yyyyMMddhhmmss');
}
});

Regards,
Witold Szczerba
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en.
>
>
Reply all
Reply to author
Forward
0 new messages