Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - Programmatically check whether filter exists

Is there a way to programmatically check whether a filter with a given name exists?

I developed a directive to process page content based on a string input, I want it to react differently in case a certain part of the string corresponds to a filter that exists in my system. For example I have a localize filter:

// Somewhere in the code
var myInput = 'localize';

// Somewhere else
var contentToProcess = 'my content';
var result = '';
if ($filter.hasOwnProperty(myInput))    // TODO: this is the part I'm trying to figure out
    result = $filter(myInput)(contentToProcess);
else
    result = 'something else';
like image 506
downhand Avatar asked Nov 22 '25 05:11

downhand


1 Answers

Jonathan's answers is also acceptable, but I wanted to find a way to check if a filter exists without using a try catch.

You can see if a filter exists like this:

return $injector.has(filterName + 'Filter');

The 'Filter' suffix is added by angular internally, so you must remember to add it or you will always return false

like image 69
DMTintner Avatar answered Nov 24 '25 20:11

DMTintner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!