Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ng-model-options in a custom directive?

Setting controller.$options in the link function seems to work for debouncing blur events:

controller.$options = {
  updateOn : 'blur',
  debounce : 3000
};

...but if I try the same for default events, the model is never updated:

controller.$options = {
  updateOn : 'default',
  debounce : 3000
};

When using the ng-model-options directive instead, everything works as expected.

See http://plnkr.co/edit/KLrSrs2Jw7pkoAUNRJDf?p=preview

like image 864
ejain Avatar asked Dec 04 '14 20:12

ejain


2 Answers

for your case you could try

ngModelController.$options = {
    updateOn: 'blur',
    updateOnDefault: true,
    debounce: {
      'blur': 2000,
      'default': 3000
    }
  };

but now (ng1.6+) you must do it like (read more here, here, and here):

ngModelController.$overrideModelOptions({
    updateOn: 'blur',
    updateOn: 'default',
    debounce: {
      'blur': 2000,
      'default': 3000
    }
});
like image 162
Xavier Haniquaut Avatar answered Oct 05 '22 00:10

Xavier Haniquaut


Looking at the source of the ngModelOptions directive, it appears that instead of setting updateOn : 'default', you need to set controller.$options.updateOnDefault = true;

like image 18
ejain Avatar answered Oct 04 '22 23:10

ejain