1

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?

Community
  • 1
  • 1
DragonKnight
  • 1,740
  • 2
  • 22
  • 35
  • 1
    Any reason you are using interpolate instead of just injecting $filter into the controller? – Sasang Apr 25 '17 at 20:14
  • @Sasang same result with the filter. is there any clean way to do it without using ng-change and calling $scope.apply every time? – DragonKnight Apr 25 '17 at 20:34
  • 1
    Are you using the nvd3 library directly? If so consider using the angular directive for it: `angular-nvd3`. In this case, just change the graph dataset when you receive/select new data, then call the refresh method on nvd3 3 attribute: ` $scope.api.refresh()` . You wouldn't need to trigger anything on ng-change or use scope.apply – Sasang Apr 25 '17 at 20:53
  • thanks for guid. let me check. – DragonKnight Apr 25 '17 at 21:40
  • @Sasang there is too much to change using angular directive. cannot do that for now. any other idea? does _searchquery updates the $scope.dataset or just filters the result? not sure how this form of filtering works in angular. – DragonKnight Apr 25 '17 at 22:03
  • 1
    Cab you update your question to include some more code, maybe a basic view and controller so i can fiddle with it. – Sasang Apr 25 '17 at 22:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142668/discussion-between-sasang-and-dragonknight). – Sasang Apr 25 '17 at 22:55
  • @Sasang if you have a look at this link (http://stackoverflow.com/questions/19849806/angular-filter-a-object-by-its-properties) I am filtering the $scope.dataset object by its multiple properties. but I cannot use that filter in the controller to update the chart. it only reflects to DOM not controller. here i am stucked. – DragonKnight Apr 26 '17 at 00:14

1 Answers1

1

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.

Community
  • 1
  • 1
DragonKnight
  • 1,740
  • 2
  • 22
  • 35