Search code examples
angularjsangularjs-ng-repeatangularjs-orderby

Is there a way to restrict the application of OrderBy on ng-repeat in AngularJs selectively?


In my application I am iterating over an array whose values that are bound to textboxes(basically input type="number"). i.e.,

say,

<div ng-repeat="number in numbers|orderBy:'value'">
     <input type="number" ng-model="number.value">
</div>  

This Plnkr link - http://plnkr.co/edit/?p=preview&s=D7hhm2al6sOXMHwQ

demonstrates what I am talking about.

So as soon as I change the value of the the textboxes are reordered. I don't want this to happen as it creates discomfort for my users who have to search for the box they just edited. I want a solution where the ng-repeat is ordered the first time the doms are created but not ordered again until they are recreated. How can I achieve this? Please help.


Solution

  • You could do it by ordering your array once in your controller, in this case by injecting $filter. Do this after you've loaded $scope.numbers, obviously.

    .controller('myController', function($filter, $scope){
        $scope.numbers = $filter('orderBy')($scope.numbers, 'value')
    })