有了对象列表,`filter`过滤器如何知道匹配哪些字段? [英] With List of objects how can the `filter` filter know which fields to match on?
问题描述
我在使用filter:query对json响应进行过滤。一切工作正常。当我把json响应转换成适当的类,过滤器停止工作。使用查询匹配类上的字段的方法是什么?这是我应该创建自己的自定义@NgFilter的情况?
I was using filter:query on a json response to filter. Everything worked fine. When I turned the json response into proper classes the filter stopped working. What is the approach to use query to match fields on a class? Is this the situation where I should then create my own custom @NgFilter?
推荐答案
对于AngularDart 过滤器
过滤器通过传递字段名作为
For the AngularDart filter
filter you specify which fields to match on by passing the field names as the keys to a map.
让我们使用 AngularDart教程 配方
类为例:
class Recipe { String id; String name; String category; ... }
您可以过滤,例如 Recipe.name
使用字符串 ctrl.nameFilterString
写:
You can filter, e.g., on Recipe.name
using the string ctrl.nameFilterString
write:
<li ng-repeat="recipe in ctrl.recipes | filter:{name:ctrl.nameFilterString}">
以下是 过滤器
API doc 如何过滤器
解释其参数。也就是说
Here is an excerpt from the filter
API doc that explains how filter
interprets its argument. I.e. in
v | filter:x
参数 x
a
-
String
,bool
和
num
:只有列表
中的项目
直接匹配此表达式即具有与此项匹配的任何
值的地图,并返回包含匹配项目的
的列表项。
String
,bool
andnum
: Only items in theList
that directly match this expression, items that are Maps with any value matching this item and items that are Lists containing a matching items are returned.
Map
:定义模式映射。对包含在输入List中的对象的特定
属性进行过滤。例如
{name:M,phone:1}
谓词将返回一个项目列表
,它们具有属性<$ c $包含M和属性电话
的c名称一个特殊的属性名称 $
可以使用
(如 {$:text}
)接受对对象的任何属性的匹配。
这相当于上面描述的
Map
: This defines a pattern map. Filters specific
properties on objects contained in the input List. For example
{name:"M", phone:"1"}
predicate will return a list of items
which have property name
containing "M" and property phone
containing "1". A special property name, $
, can be used
(as in {$: "text"}
) to accept a match against any property of the object.
That's equivalent to the simple substring match with a String
as
described above.
这篇关于有了对象列表,`filter`过滤器如何知道匹配哪些字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!