【发布时间】:2014-02-01 14:13:39
【问题描述】:
我需要从我的控制器访问已在模板(在 ng-repeat 内)过滤的数据。
这就是我的意思:
我的模板中有这张表:
<table class="table">
<thead>
<th>Name</th>
<th>Gender</th>
</thead>
<tbody>
<tr ng-repeat="person in persons | filter:query">
<td>{{person.name}}</td>
<td>{{person.gender}}</td>
</tr>
</tbody>
</table>
然后我有一个<select>,用于按性别过滤数据
<h1>Cluster:</h1>
<select ng-model="query.gender" >
<option value ="">ALL</option>
<option value ="male">Male</option>
<option value ="female">Female</option>
</select>
这没问题。
如果有人选择了其中一个选项,我需要对过滤后的结果做一些事情。 这就是为什么我的控制器中有一个 $watch 来查找过滤器查询的更改:
$scope.$watch('query.gender', function(newValue, oldValue) {
// Here is where I need to get filtered results
});
我的问题是:
如何从控制器访问过滤后的内容?
我希望这样最好不必在控制器中进行另一个“过滤”操作......(因为数据已经被过滤了,结果就在内存中的某个地方,对吧?)
【问题讨论】:
-
我不认为结果存在于内存中,不。 ng-repeat 过滤集合并为每个接受的人生成一个 dom 元素,仅此而已。
-
感谢@JBNizet 的帮助
标签: angularjs