In angular, you can write filter expressions like
<input type="text" ng-model="query"> <table> <tr ng-repeat="phone in phones | filter: query"> <td>{{phone.vendor}}</td> <td>{{phone.model}}</td> </tr> </table>
and it will update the table to show only the phones which match the query
text you enter into the input
.
How can I get the corresponding result array of the filter, e.g. the [phone object]
s that are currently displayed, in a variable (e.g. a scope variable)?
Filters can be added to expressions by using the pipe character | , followed by a filter.
The “filter” Filter in AngularJS is used to filter the array and object elements and return the filtered items. In other words, this filter selects a subset (a smaller array containing elements that meet the filter criteria) of an array from the original array.
You can actually assign new variables to the scope in an angular expression. So the simplest solution would be to do <tr ng-repeat="phone in (filteredPhones = (phones | filter: query))">
. filteredPhones is now a variable in the current scope - see this plnkr example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With