Don’t forget your dependencies – filters that need $filter

I needed to wrap angularjs’s date filter in a fairly simple manner, but I rushed it initially and skipped some very basic steps. The following error occurred.

Can’t interpolate ReferenceError: $filter is not defined

app.filter('emptyStringIfDateBefore1900', function() {
  return function(input) {
    return $filter('date')(input, 'MM/dd/yyyy');
  }
});

This is really straightforward to resolve, you just need to inject $filter.

app.filter('emptyStringIfDateBefore1900', ['$filter',function($filter) {
  return function(input) {
    return $filter('date')(input, 'MM/dd/yyyy');
  }
}]);

Leave a Reply

Your email address will not be published. Required fields are marked *