2

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?

0

1 Answer 1

3

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);
    }
  }]);
}]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.