使用对象列表,“过滤器"过滤器如何知道要匹配哪些字段? [英] With List of objects how can the `filter` filter know which fields to match on?
问题描述
我在 json 响应上使用 filter:query 来过滤.一切正常.当我将 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 filter
filter 您可以通过将字段名称作为传递来指定要匹配的字段地图键.
For the AngularDart filter
filter you specify which fields to match on by passing the field names as the keys to a map.
让我们使用 AngularDart 教程 食谱代码>类为例:
Let us use the AngularDart tutorial Recipe
class as an example:
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}">
这里是摘自 filter
API doc 解释了 filter
如何解释其参数.IE.在
Here is an excerpt from the filter
API doc that explains how filter
interprets its argument. I.e. in
v | filter:x
参数 x
可以是一个
String
、bool
和num
:只有List
中的项目直接匹配这个表达式,任何 Maps 的项目与此项目和包含一个列表的项目匹配的值返回匹配项.
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
:这定义了一个模式映射.过滤器特定输入列表中包含的对象的属性.例如{name:"M", phone:"1"}
谓词将返回项目列表其中具有包含M"的属性 name
和属性 phone
包含1".可以使用特殊的属性名称 $
(如在 {$: "text"}
中)接受与对象的任何属性的匹配.这相当于简单的子串与 String
匹配,如如上所述.
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.
这篇关于使用对象列表,“过滤器"过滤器如何知道要匹配哪些字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!