我需要从控制器访问已在模板(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>用于按性别过滤数据的
<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 });
我的问题是:
如何从控制器访问FILTERED内容?
我最好这样,而不必在控制器中执行其他“过滤”操作…(由于数据已经被过滤,结果位于内存中的某个地方,对吗?)
我不确定,但是您可以在控制器中使用过滤器功能。因此,尝试类似:
$scope.$watch('query.gender', function(newValue, oldValue) { var x = $filter('filter')($scope.persons, $scope.query); });