Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I obtain the result array of an angular "| filter" expression in a variable?

Tags:

angularjs

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)?

like image 375
nh2 Avatar asked Apr 17 '13 02:04

nh2


People also ask

What is the correct way to apply filter in angular?

Filters can be added to expressions by using the pipe character | , followed by a filter.

What does the AngularJS filter filter do?

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.


1 Answers

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.

like image 182
joakimbl Avatar answered Nov 09 '22 05:11

joakimbl