AngularJS Filter Filter

Definition and Usage

filter Filters allow us to filter arrays and return an array that only contains matching items.

This filter can only be used for arrays.

Related Pages

AngularJS Tutorial:Angular Filters

Instance

Example 1

Display items containing the letter "A":

    <li ng-repeat="x in cars | filter : 'A'">{{x}}</li>

亲自试一试

Example 2

Use an object as a filter:

    <li ng-repeat="x in customers | filter : {'name' : 'O', 'city' : 'London'}"> {{x.name + ", " + x.city}}

亲自试一试

例子 3

进行“严格”比较,除非值与表达式完全相同,否则不会返回匹配项:

  • {{x.name + ", " + x.city}}

亲自试一试

语法

{{ arrayexpression | filter : expression : comparator }}

Parameters

Parameters Description
expression

The expression used to select items from an array. The type of the expression can be:

String: Returns array items that match the string.

Object: An object is the pattern searched in the array. For example: filter: {"name" : "H", "city": "London"} will return array items whose names contain the letter "H" and whose cities contain "London". See the example above.

Function: A function that will be called for each array item, and the items that return true will appear in the result array.

comparator

Optional. Define the strictness of comparison. The value can be:

true: Only returns the match item when the value of the array item is exactly the same as the value we want to compare.

false: Returns the match item if the value of the array item contains the value to be compared. This comparison does not distinguish between uppercase and lowercase. This is the default value.

Function: We can define a function to determine what is considered a match or not.