climboid
climboid

Reputation: 6952

angularJS orderBy not reflected in original scope

so I have a scope.data with an array of my objects. The data of that array is beeing displayed in a table and it is orderedBy x property. The display in the table works perfectly but the scope.data object its self is not sorted to reflect what the table is showing... Is there any way to get that object also sort as the sorting options?

Upvotes: 1

Views: 955

Answers (2)

Alex Osborn
Alex Osborn

Reputation: 9851

Instead of applying the filter to scope.data in your template, you could apply the filter in your controller instead:

Instead of:

Controller:

$scope.data = [data array];

Template:

{{ data | orderBy:x }}

Change to:

Controller:

// Inject $filter into controller
$scope.data = $filter('orderBy')('x');

Template:

{{ data }}

Data binding:

If your data array was passed in, you can handle updates by setting up a watch function to check if the original array changes, and then automatically applying to the current $scope.data property.

Upvotes: 3

Christian Stewart
Christian Stewart

Reputation: 15519

OrderBy does not sort the scope's variable, instead it sorts the way it is displayed. You will need to sort the array in javascript if you want it to change in the $scope.

Upvotes: 3

Related Questions