角函数过滤器比较器示例 [英] Angular function filter comparator example

查看:26
本文介绍了角函数过滤器比较器示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我一个如何使用 Angular 过滤器比较器的例子?

Can someone give me an example of how to use Angular filter comparator?

来自官方文档:

function(actual, expected):该函数将被赋予对象值和谓词值进行比较,如果该项目应包含在过滤结果中,则应返回true.

function(actual, expected): The function will be given the object value and the predicate value to compare and should return true if the item should be included in filtered result.

有一篇很棒的博客文章讨论了 Angular 过滤器:有趣的 AngularJS 过滤器 - 第 1 部分:<代码>过滤器过滤器

There is a great blog post talking about Angular filter: Fun with AngularJS filters - Part 1: the filter filter

然而,在它的最后,我正在寻找一些有用的函数比较器示例,但我仍然一无所获.

However, at the end of it, where I am looking for some useful example of the function comparator, I still found nothing.

对于更特殊的匹配需求,您可以传递一个函数而不是布尔值作为比较器参数.

For more particular matching needs, you can pass a function instead of a boolean as the comparator argument.

我自己尝试了几种组合.在表达式末尾添加函数或指向作用域中的函数都不起作用.

I have tried few combinations by myself. Neither adding the function at the end of the expression or pointing to a function in scope do the work.

推荐答案

我做了这样的事情,因为 Angular 迭代对象并尝试比较它们的整体以及递归它们的各个属性.

I did something like this, since Angular iterates on objects and tries to compare them whole as well as recursively their individual properties.

// in your controller
$scope.filterMyData = function (input, search_param) {
  if (input && input.propertyWeCareAbout) {
    // it's ugly, but to quickly get something done you can ignore search_param 
    return input.propertyWeCareAbout === $scope.SomeOtherData;
  }
  else {
    return angular.equals(input, search_param);
  }
}

<span>Quick search: </span><input type="search" ng-model='quickSearch'/><br/>


<table>
  <tr ng-repeat="obj in someHttpService.dataAsArray | filter:quickSearch:filterMyData">
    <td>{{ obj.key }} </td>
    <td>{{ obj.propertyWeCareAbout }}</td>
  </tr>
</table>

就我而言,quickSearch"在很大程度上毫无意义,过滤是由不同的逻辑完成的.您始终可以通过在 ng-repeat 中添加| filter:quickSearch"来在最后链接另一个过滤器.

In my case, "quickSearch" is largely meaningless, and filtering was done by a different logic. You can always just chain another filter at the end by adding "| filter:quickSearch" into ng-repeat.

这篇关于角函数过滤器比较器示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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