Profile picture Schedule a Meeting
c a n d l a n d . n e t

AngularJS dynamic filter

Dusty Candland | | AngularJS, javascript

Use this to dynamically pass a filter from a controller or somewhere else as a string. It will use DI to lookup the filter and apply it.

In the template

row.label | date | dynamic:nameFilter

In the controller

$scope.nameFilter = 'mycustomerfilter';
app.filter('dynamic', ['$injector', function($injector) {
  return function(text, filter) {
    var result = text;
    if (filter) {
      var f = $injector.get(filter + "Filter");
      if (f) { result = f(text); }
    }
    return result;
  }
}]);

Webmentions

These are webmentions via the IndieWeb and webmention.io. Mention this post from your site: