coxx coxx - 4 months ago 39
AngularJS Question

How to extend or override existing filters in angularjs?

Is it possible to extend existing "standard" filters (

In my case I need to parse date from YYYYMMDDhhmmss format so I'd like to extend (or override)
filter instead of writing my own.


I'm not sure if I understand your question correctly, but if you would like to extend functionality of existing filters you could create a new filter that decorates an existing one. Example:

myApp.filter('customDate', function($filter) {
    var standardDateFilterFn = $filter('date');
    return function(dateToFormat) {
     return 'prefix ' + standardDateFilterFn(dateToFormat, 'yyyyMMddhhmmss');

and then, in your template:

{{now | customDate}}

Having said the above, if you simply want to format a date according to a given format this can be done with the existing date filter:

{{now | date:'yyyyMMddhhmmss'}}

Here is the working jsFiddle illustrating both techniques:

Please note that if a format is not specified AngularJS will assume that this is 'medium' format (the exact format depends on a locale). Check for more.

The last remark: I'm a bit confused about the 'parse from' part of your question. The thing is that filters are used to parse an object (date in this case) to string and not vice verse. If you are after parsing strings (from an input) representing dates you would have to look into NgModelController#$parsers (check the "Custom Validation" part in