输入输入时Angularjs暂停

时间:2015-06-10 18:16:23

标签: javascript jquery angularjs

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

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

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

我想我需要使用ng-change或类似于我输入区域的东西,但我是棱角分明的新人,我们将非常感谢任何建议。

2 个答案:

答案 0 :(得分:2)

找到答案。如果3000是您要等待的毫秒数,则可以执行此操作。

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

答案 1 :(得分:0)

编辑:@bryan有正确答案!

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

工作人员:

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>