如何逃脱'!'当调用angularjs过滤器时 [英] How to escape '!' when call angularjs filter

查看:66
本文介绍了如何逃脱'!'当调用angularjs过滤器时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AngularJS过滤器保护了一些特殊字符.可以通过在字符串前面加上来否定谓词,但是现在我想过滤掉包含'!'的项目.特殊字符我如何才能逃脱此特殊字符'!'请吗?

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?

推荐答案

您无法逃脱它,因为 filter filter

You can't escape it, because filter filter doesn't presume it.

但是您可以使用谓词功能覆盖的默认行为!谓词和修饰 filter 而不破坏其他任何内容.

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);
    }
  }]);
}]);

这篇关于如何逃脱&amp;#39;!&amp;#39;当调用angularjs过滤器时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆