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?
问题:
回答1:
For the AngularDart filter
filter you specify which fields to match on by passing the field names as the keys to a map.
Let us use the AngularDart tutorial Recipe
class as an example:
class Recipe { String id; String name; String category; ... }
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}">
Here is an excerpt from the filter
API doc that explains how filter
interprets its argument. I.e. in
v | filter:x
the argument x
can be a
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
: 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 propertyname
containing "M" and propertyphone
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 aString
as described above.
回答2:
I'm not sure what you mean by 'filter:query'. I guess it is the filter filter
.
filter
seems to convert the items in the collection to string and compares them with the search text.
If the toString()
doesn't contain the field you want to compare it won't match.
A custom filter is probably the best option.
Example:
angular.dart.tutorial / Chapter_07 / lib / filter / category_filter.dart