Angular js: Access data filtered in ng-repeat (ngRepeat) from controller

前端 未结 2 865
无人共我
无人共我 2020-12-31 23:54

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

Here\'s what I mean:

I have this table in my template:

相关标签:
2条回答
  • 2021-01-01 00:39

    You can simplify your code with the following:

        <tbody>
            <tr ng-repeat="person in (filteredPersons = (persons | filter:query))">
                <td>{{person.name}}</td>
                <td>{{person.gender}}</td>
            </tr>
        </tbody>
    

    After that, you can get access to $scope.filteredPersons into controller or {{filteredPersons}} right into the view :)

    0 讨论(0)
  • 2021-01-01 00:50

    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 should then contain the same filtered data as in your table I think. Have a look at the docs here: http://docs.angularjs.org/api/ng.filter:filter for some more info.

    0 讨论(0)
提交回复
热议问题