Disabling orderBy in AngularJS while editing the list

前端 未结 8 650
情歌与酒
情歌与酒 2021-01-07 23:06

After applying orderBy in my list of input boxes if i edit any field it starts sorting immediately and i loose focus. I tried to dug in the angular code and fou

8条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-07 23:44

    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());
                    });         
                });
            }
        };
    });
    
    
    

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

提交回复
热议问题