How to use an object that is filtered by its properties in angular controller?

81 Views Asked by At

I am illustrating a stacked graph along with its table and fetching its json data with $http.get() and setting it to $scope.dataset

html:

<input ng-model="_searchQuery.key1.key2">
<tr  ng-repeat="(key, value) in dataset | filter:_searchQuery ">

I how can I update $scope.dataset in controller?

_searchQuery matches dataset and filters my table like charm. no need to code any thing in controller. ie dataset.key1.key2 reflects to _searchQuery.key1.key2

but for my nvd3.js stacked graph I have to filter the $scope.dataset the same way in DOM. $scope.dataset = $filter('$scope._searchQuery.key1.key2')($scope.dataset) throughs an err. Im following angular documentation and this link: How to use a filter in a controller?

1

There are 1 best solutions below

0
DragonKnight On

It might be a possible duplicate question How do I filter an array with AngularJS and use a property of the filtered object as the ng-model attribute?

same solution worked for me:

first: I had to instantiate $scope._searchQuery in controller as an object and include its properties.

second: update the $scope.dataset - and the keyword for filter by object property is filter which I was playing around with _searchQuery

the syntax helped me to fix the problem: $scope.dataset = $filter('filter')($scope.dataset,$scope._searchQuery.key1.key2);

It looks its not possible to pass more than 2 args to $filter, thus to filter based on more inputs I had to copy the same line and pass $scope._searchQuery.key1.key3 to it.