曾经蜡笔没有小新2017-05-15 16:58:17
漂亮男人2017-05-15 16:58:17
根據官方文件的描述,有幾種方式來實現:
{{ filter_expression | filter : expression : comparator}}
1.如Guox給出的方法,使用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 itemsms whichnty name" property phone containing "1".
作為Object使用,用於過濾數組元素的指定屬性,如expression = {name:"M", phone:"1"} ,那麼將會過濾出數組中name包含'M'且phone包含'1'的元素。
故使用以下方法即可
<li ng-repeat="phone in phones|filter:{name:query}">
2.可以使用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, expect ): match.
透過Comparator的回傳值能夠決定是否匹配,入參分別是actual(陣列中的元素)和expected(輸入)
那麼定義一個函數🎜
$scope.customerFilter = customerFilter;
function customerFilter(actual, expected){
return actual.name.contains(expexted) ? true : false;
}
前端使用
<li ng-repeat="phone in phones|filter:query:customerFilter">