2015-06-10 62 views
0

现在我正在使用jQuery来确定输入何时开始输入,然后暂停。Angularjs在输入中输入时暂停

var timeoutId; 
$('#container').on('input propertychange change', 'form', function(e) { 
    clearTimeout(timeoutId); 
    timeoutId = setTimeout(function() { 
     console.log("paused"); 
    }, 3000); 
}); 

我正在尝试找到一个合适的Angularjs解决方案来完成此任务。

我想我需要使用ng-change或类似于我的输入区域,但我是新来的角和任何建议将不胜感激。

回答

2

找到答案。你会这样做,其中3000是你想要等待的毫秒数量。

<input ng-change="functionToRun()" ng-model-options="{debounce: 3000}" /> 
0

编辑:@bryan有正确的答案!

您可以对输入和$ timeout服务使用ng-change指令来处理暂停事件。

工作plunker:

http://plnkr.co/edit/Y6NtjabxeGaQpOIif1U3?p=preview

<body ng-app="inputExample"> 
    <script> 
    angular.module('inputExample', []) 
     .controller('ExampleController', ['$timeout', '$scope', function($timeout, $scope) { 
     $scope.checkPause = function() { 
      $timeout.cancel($scope.pause); 
      $scope.pause = $timeout(function() { 
      alert("pause"); 
      }, 1000); 
     }; 
     }]); 
    </script> 
    <div ng-controller="ExampleController"> 
    <form name="myForm"> 
     <label> 
     User name: 
     <input ng-change="checkPause()" type="text" name="userName" ng-model="user.name" required> 
     </label> 
    </form> 
    </div> 
</body>