我怎样才能获得一个角度的结果数组 "|过滤器"变量中的表达式? [英] How can I obtain the result array of an angular "| filter" expression in a variable?
问题描述
在 angular 中,您可以编写过滤器表达式,例如
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>
并且它将更新表格以仅显示与您在 input
中输入的 query
文本匹配的手机.
and it will update the table to show only the phones which match the query
text you enter into the input
.
如何获取过滤器对应的结果数组,例如当前显示的 [phone object]
在变量(例如范围变量)中?
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)?
推荐答案
您实际上可以在角度表达式中为作用域分配新变量.因此,最简单的解决方案是执行 <tr ng-repeat="phone in (filteredPhones = (phones | filter: query))">
.filteredPhones 现在是当前范围内的一个变量 - 请参阅这个 plnkr 示例.
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.
这篇关于我怎样才能获得一个角度的结果数组 "|过滤器"变量中的表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!