一种制造角滤波器条件 [英] Making an angular filter conditional

查看:126
本文介绍了一种制造角滤波器条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的文本输入来过滤项的列表。我想使它所以当一个特定的变量设置,列表不会过滤,无论文本输入是什么。我如何能做到这一点有什么建议?

 <一个NG重复=数据集|过滤器:{值:搜索}数据ID ={{set.id}}NG-鼠标按下= setBox(设置)NG-鼠标悬停=的setSelected(套,$事件)NG绑定-HTML =set.value | trustHTML>< / A>


解决方案

可以做到这一点,如果你设置过滤器前pression到(或未定义) - 这会导致过滤器不适用 - 当你的 disableFilter 设置,或实际过滤前pression否则

因此​​,假设,这种切换变量 - disableFilter - 是一个布尔值:

 <一个NG重复=数据集|过滤器:(!disableFilter ||'')及和放大器; filterEx pression>

(用 filterEx pression 是要过滤通过任何除权pression)。您的具体情况是:

 <一个NG重复=数据集|过滤器:(disableFilter ||''!)及和放大器; {值:搜索}>

编辑:

要解释以上是如何工作的。


  1. 记住 || &放大器;&安培; 返回它的其中一个操作数的值

  2. || &放大器;&安培; 使用短路评价 - 真| | (任何)收益真正; 假放;&安培; (任何)收益 - 无需评估(任何)前pression

  3. 是falsy(或使用未定义相反,如果它更清晰)

因此​​,

disableFilter ===真正!disableFilter ===虚假,这样的第二个操作数 || - 空字符串! - 评估(这是falsy)和(disableFilter ||'')收益 - 一个falsy值,短路&放大器;&安培; 操作,不评估&放大器的第二个操作数;&安培; 。恩pression的返回值是这样

disableFilter ===虚假!disableFilter ===真正,其中短路 || 操作,那么&放大器的第二个操作数;&安培; 评估并返回。恩pression的返回值是这样 {值:搜索}

了解更多关于<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators\">logical这里运营商

I'm currently using a text input to filter a list of items. I'd like to make it so when a particular variable is set, the list doesn't filter, regardless of what the text input is. Any advice on how I can accomplish this?

<a ng-repeat="set in data | filter: { value: search }" data-id="{{set.id}}" ng-mousedown="setBox(set)" ng-mouseover="setSelected(set, $event)" ng-bind-html="set.value | trustHTML"></a>

解决方案

You can achieve this if you set the filter expression to '' (or undefined) - this causes the filter not to be applied - for when your disableFilter is set, or to the actual filter expression otherwise.

So, assuming, this toggling variable - disableFilter - is a boolean :

<a ng-repeat="set in data | filter: (!disableFilter || '') && filterExpression">

(with filterExpression being whatever the expression you want to filter by). Your specific case would be:

<a ng-repeat="set in data | filter: (!disableFilter || '') && {value: search}">

EDIT:

To explain how the above works.

  1. Remember that || and && return the value of one of its operands.
  2. || and && use short-circuit evaluation - true || (anything) returns true; false && (anything) returns false - without evaluating the (anything) expression.
  3. '' is falsy (or use undefined instead, if it's clearer)

And so,

when disableFilter === true, !disableFilter === false, thus the second operand of || - the empty string '' - is evaluated (it's falsy), and (!disableFilter || '') returns '' - a falsy value, which short-circuits the && operation and does not evaluate the second operand of &&. The return value of the expression is thus ''.

when disableFilter === false, !disableFilter === true, which short-circuits the || operation, then the second operand of && is evaluated and returned. The return value of the expression is thus {value: search}.

Read more about logical operators here

这篇关于一种制造角滤波器条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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