javascript - AngularJS - a way to timeout html input for a second and then call $watch -


For example: I am typing in the input text, which has a NG-model, and $ Watch is called instantaneously. Is there any way to call $ later, suppose that 2 seconds has passed with the last key pressed on input control?

I wrote a simple instruction for such works: consider this code:

  .directive ('delayedModel', function () {return {scope: {model: '= delayedModel'}, link: function (scope, element, attrs) {element .val (scope.model) ; Scope $ watch ('model', function (newVal, oldVal) {if (newVal == oldVal) {element.val (scope.model);;}}) timeout; element .on ('KeyUp Paste Search ', Function () {clearTimeout}; timeout = setTimeout (function () {scope.model = element [0] .value; element.val (scope.model); scope $ apply ();}, attrs Delay || 500);});}};});   

Usage:

  & lt; Input type = "text" delay-model = "search text" data-delay = "500" />   

So the only thing that you need to change, you use the delay-model instead of ng-model . Demo:

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -