Disabling orderBy in AngularJS while editing the list Disabling orderBy in AngularJS while editing the list angularjs angularjs

Disabling orderBy in AngularJS while editing the list


I was using orderBy on a list that could be re-ordered using angular-sortable, and ran into a similar issue.

My solution was to perform the ordering manually, by calling the orderBy filter inside the controller when the page was initialised, and then calling it subsequently when necessary, for example in a callback function after re-ordering the list.


You could override the directive to change the moment of the update to the moment you wish the reordering. You could also just not use ng-model and rely on a custom directive.

This thread discuss overriding the input directive to change the model update to be triggered by tge blur event. Take a look at the fiddle.

Although you might override the directive, you shouldn't do this, and the best solution, as explained and exemplified by @Liviu T. in the comments below would be to create a custom directive that removes the event keyup binding and adds a blur one. Here is directive code, and here is Liviu's plunker:

app.directive('modelChangeBlur', function() {    return {        restrict: 'A',        require: 'ngModel',            link: function(scope, elm, attr, ngModelCtrl) {            if (attr.type === 'radio' || attr.type === 'checkbox') return;            elm.unbind('input').unbind('keydown').unbind('change');            elm.bind('blur', function() {                scope.$apply(function() {                    ngModelCtrl.$setViewValue(elm.val());                });                     });        }    };});
<input type="text" ng-model="variable" model-change-blur/>

Unfortunately, as Angular events are not namespaces, you will have to remove any previously added event.


A different approach may be to not loose focus to begin with. If your main problem is that your loosing focus, then instead of disabling orderBy, add this directive to your input:

app.directive("keepFocus", ['$timeout', function ($timeout) {    /*    Intended use:        <input keep-focus ng-model='someModel.value'></input>    */    return {        restrict: 'A',        require: 'ngModel',        link: function ($scope, $element, attrs, ngModel) {            ngModel.$parsers.unshift(function (value) {                $timeout(function () {                    $element[0].focus();                });                return value;            });        }    };}])

Then just:

<input keep-focus ng-model="item.name"/>

I know this does not directly answer you question, but it might help solve the underlying problem.