angular.js - 关于angularjs中filter问题
滿天的星座
滿天的星座 2017-05-15 16:56:17
0
3
569

<li ng-repeat="phone in phones|filter:query">

一般都是这样写过滤,但是我想只针对一部分数据,phone.name实现过滤这个要如何实现呢??

滿天的星座
滿天的星座

reply all(3)
曾经蜡笔没有小新

<li ng-repeat="phone in phones|filter:{'name': query}">

漂亮男人

According to the official documentation, there are several ways to achieve this:
{{ filter_expression | filter : expression : comparator}}

1. As the method given by Guox, use expression:

Object: A pattern object can be used to filter specific properties on objects contained by array. For example {name:"M", phone:"1"} predicate will return an array of items which have property name containing "M" and property phone containing "1".

Used as an Object, used to filter specified attributes of array elements, such as expression = {name: "M", phone: "1"}, then the array whose name contains 'M' and phone contains '1' will be filtered out element.

So just use the following method

<li ng-repeat="phone in phones|filter:{name:query}">

2. You can use comparator:

Comparator which is used in determining if the expected value (from the filter expression) and actual value (from the object in the array) should be considered a match.
function(actual, expected): The function will be given the object value and the predicate value to compare and should return true if both values ​​should be considered equal.

The return value of Comparator can be used to determine whether there is a match. The input parameters are actual (elements in the array) and expected (input)

Then define a function

$scope.customerFilter = customerFilter;

function customerFilter(actual, expected){
    return actual.name.contains(expexted) ? true : false;
}

前端使用
<li ng-repeat="phone in phones|filter:query:customerFilter">
習慣沉默

Thank you both so much, thank you

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template