AngularJS过滤器比较,而真正显示NG重复列表时输入字段为空 [英] AngularJS filter comparator true while displaying ng-repeat list when input field is empty

查看:170
本文介绍了AngularJS过滤器比较,而真正显示NG重复列表时输入字段为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个例子拨弄它演示了如何使用比较参数的过滤精确匹配....都会响起:

I'm going off by this example fiddle where it demonstrates the use of comparator parameter to filter exact matches....:

http://jsfiddle.net/api/post/library/pure/

当务之急是从1-100的数字,但我把它输入文本和过滤字符串作为
从而使包括一个子也将通过NG重复...当我键入1像任何的数据也显示11,111,132等...这是我穿过怎么来的:真的比较。

priority is a number from 1-100, but I have it input as text and filtered as string so any data that includes a substring will also pass through the ng-repeat...like when I type 1 it will also display 11, 111, 132 etc...which is how I came across the :true comparator.

我读过其他stackflow答案提示编写自定义过滤器功能,但与真正的比较,它看起来像我能达到我想要的只是:

I've read other stackflow answers suggesting to write custom filter functions but with the true comparator it looks like I can achieve what I want just by:

<input type="text" class="form-control" id="search.priority"
  title='Priority number to filter by'
  ng-model="search.priority" >

<tr ng-repeat="workflowItem in workflows | filter:search:true">
  <td>{{workflowItem.priority}}</td>

如果它确实只能过滤精确匹配。但是,显然它并不当输入字段为空,因为没有匹配空字符串传递什么。

where it does only filter the exact matches. However, obviously it does not pass anything when the input field is empty since nothing matches the empty string.

我的问题是:有没有一种方法,我可以让NG-重复时,该字段为空,同时保持精确匹配滤波器还显示一切吗?鸭preciate大家的时间!

My question is: Is there a way I can allow ng-repeat to still display everything when the field is empty while keeping the exact match filter? Appreciate everyone's time!

推荐答案

您需要使用自定义的比较器功能。它可以让你执行除了当predicate是falsy进行严格比较。

You'll want to use a custom comparator function. It will allow you to perform a strict comparison except when the predicate is falsy.

您标记随后将是:

 <input type="text" class="form-control" id="search.priority"
  title='Priority number to filter by'
  ng-model="search.priority" >

<tr ng-repeat="workflowItem in workflows | filter:search:exceptEmptyComparator">
 <td>{{workflowItem.priority}}</td>

和定义控制器上的比较器功能:

And define the comparator function on your controller:

$scope.exceptEmptyComparator = function (actual, expected) {
    if (!expected) {
       return true;
    }
    return angular.equals(expected, actual);
}

这是应该做的伎俩。

这篇关于AngularJS过滤器比较,而真正显示NG重复列表时输入字段为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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