1

Hi Im attempting to build functionality around the length of a filter in angularjs, and although its working as it should in the view, in the controller the variable seems to stay outdated...

When I click on the div below it filters a list and calls the filterby function. The output of the length of the newly filtered list updates in the view correctly. However in the function itself I have a log set and it is still showing the old length when I click on the div.

<div ng-repeat="filter in filters" ng-click="filterby(filter.filter_type)">{{filter.filter_type}}</div>

<ul>
    <li ng-repeat="event in filtered = (events | filter:query) | orderBy:'-event_date' ">
        <span >{{event.event_date}},{{event.event_name}}, {{event.event_venue}}, {{event.event_description}} </span>
    </li>
</ul>

<br />Length of filtered items {{filtered.length}}

And my view....

        $scope.filterby = function(filterby) {
          if (filterby == 'ALL') {
            $scope.query = '';
          }
          else {
            $scope.query = filterby;

          }
          console.log($scope.filtered.length);
    };  

My filter data:

        $scope.filters = [
         {'filter_type' : 'ALL'},
         {'filter_type' : 'Macnass'}
        ];

EDIT: Ok its not that it nots working at all, its just showing the previous value, as if its one click behind all the time, so its something to do with the fact that the variable in the view is updated after the list is made. but Im not sure how to go about insuring the variable in the controller is the latest value.

Plunker: http://plnkr.co/edit/u7KpqYx8gDwaaXEvGeMn?p=preview

3
  • can you setup a plunker, showing the problem... Commented Sep 28, 2014 at 12:08
  • I have added a plunker, upon doing that I can see clearly what the problem is, i.e the console.log is showing before the new filter is applied (I think), however Im unsure how to fix this. ( I should add that Im quite new to angularjs) Commented Sep 28, 2014 at 12:32
  • probably better to just do your filtering in controller. Then you have access to original array and filtered array Commented Sep 28, 2014 at 12:36

1 Answer 1

1

check out the plunker

added below

$scope.filtered = $filter('filter')($scope.events, $scope.query)

in $scope.filterby function

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that sorted the issue!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.