角JS:访问数据以ng重复(ngRepeat)从过滤器 [英] Angular js: Access data filtered in ng-repeat (ngRepeat) from controller

查看:82
本文介绍了角JS:访问数据以ng重复(ngRepeat)从过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的控制器访问已过滤的模板(一纳克重复内)的数据。

I need to access data that was already filtered on a template (inside a ng-repeat) from my controller.

这就是我的意思是:

我有这个表我的模板:

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

然后,我有一个&LT;选择&GT; 是用来过滤数据性别

<h1>Cluster:</h1>
    <select ng-model="query.gender" >
        <option value ="">ALL</option>
        <option value ="male">Male</option>
        <option value ="female">Female</option>
    </select>

这工作正常。

如果有人选择的选项之一,我需要做的筛选结果的东西。
这就是为什么我在我的控制器中的$手表,看起来更改到过滤器查询:

If someone selects one of the options, I need to do something with the filtered results. That's why I have a $watch in my controller that looks for changes to the filter query:

$scope.$watch('query.gender', function(newValue, oldValue) {
            // Here is where I need to get filtered results
});

我的问题是:

我如何从控制器访问过滤的内容?

How do I access the FILTERED content from the controller?

我想这preferably无需做另一个过滤操作的控制器......(因为数据已经被过滤,其结果是坐在某个位置在内存中,对吧?)

I'd like this preferably without having to do another "filtering" operation in the controller... (since the data was already filtered, the result is sitting somewhere in memory, right?)

推荐答案

我不能完全肯定,但你可以在你的控制器使用过滤功能。因此,尝试这样的:

I'm not entirely sure, but you can use the filter function in your controller. So try something like:

$scope.$watch('query.gender', function(newValue, oldValue) {
        var x = $filter('filter')($scope.persons, $scope.query);
});

然后

x应包含相同的过滤后的数据在你的表,我认为。看看这里的文档:<一href=\"http://docs.angularjs.org/api/ng.filter:filter\">http://docs.angularjs.org/api/ng.filter:filter更多的一些信息。

这篇关于角JS:访问数据以ng重复(ngRepeat)从过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆