How can I make Angular re-evaluate an orderBy function when it needs to?

3,632 views
Skip to first unread message

Jon Rimmer

unread,
Mar 11, 2013, 2:43:20 PM3/11/13
to ang...@googlegroups.com
Hi,

I have an ng-repeat with an orderBy filter to sort it. The orderBy filter is supplied a function that extracts a property for sorting. The function also uses another property, on the parent scope, to control exactly what property is extracted.

Simplified, pseudo-code:

<div ng-repeat="item in items | orderBy:myFilterFunction">
   ...
</div>

function Controller() {
   ...
   $scope.whichPropertyToSortBy = 'x';

   $scope.myFilterFunction = function(item) {
       if ($scope.whichPropertyToSortBy == 'x') {
           return some calculated expression;
       }
       else
       {
           return some other calculated expression;
       }
   }
}

The problem is, when $scope.whichPropertyToSortBy is changed, as the result of some other action like a button click, Angular does not reorder the items. How can I arrange things so Angular realises that myFilterFunction has a dependency on whichPropertyToSortBy? Or can I force an update of the ng-repeat somehow?

I've come to Angular from Knockout, where it's fairly trivial to express these kind of dependencies. It seems like Angular's dirty-checking works well for models with primitive properties, but not ones where things need to be computed dynamically. Maybe I just need to shift my mindset as to how to handle these sort of things in Angular?

Pawel Kozlowski

unread,
Mar 11, 2013, 2:55:08 PM3/11/13
to ang...@googlegroups.com
Hi!

This sounds strange, IMO it should work without any special actions
from developer's part.
Could you put your code in http://plnkr.co/ and post here?

Cheers,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to angular+u...@googlegroups.com.
> To post to this group, send email to ang...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en-US.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Looking for bootstrap-based widget library for AngularJS?
http://angular-ui.github.com/bootstrap/

Dave Merrill

unread,
Mar 12, 2013, 1:04:31 PM3/12/13
to ang...@googlegroups.com
That sounds very much like the demo in the docs for orderBy, which does work: http://docs.angularjs.org/api/ng.filter:orderBy

Dave Merrill

Florian Orben

unread,
Mar 12, 2013, 3:32:44 PM3/12/13
to ang...@googlegroups.com
Hey,

how are you updating the $scope.whichPropertyToSortBy property?
Is the change being triggered by an external source (like a jquery plugin) - so that you'd need to wrap that in an $scope.$appy call?
Otherwise a $scope.$digest() should do the trick I guess...

A plnkr would be useful though..

binyamin schein

unread,
Dec 17, 2015, 5:48:10 AM12/17/15
to AngularJS
hey jon, have you solved this? i am having the same problem
Reply all
Reply to author
Forward
0 new messages