AngularJS filter protected some special chars. The predicate can be negated by prefixing the string with !, but now I want to filter out the items which contains the '!' special char. How can I escapes this special char '!' please?
1 Answer
You can't escape it, because filter filter doesn't presume it.
But you can use predicate function to override default behaviour for ! predicate and decorate filter without breaking anything else.
app.config(['$provide', function ($provide) {
function bang(value) {
return value.indexOf('!') >= 0;
};
function bangless(value) {
return value.indexOf('!') < 0;
};
$provide.decorator('filterFilter', ['$delegate', function ($delegate) {
return function (array, expression, comparator) {
if (expression === '!') {
expression = bang;
} else if (expression === '!!') {
expression = bangless;
}
return $delegate(array, expression, comparator);
}
}]);
}]);