[Angularjs输入输入时暂停

问题描述 投票:0回答:2

我正在使用jQuery找出输入何时开始输入然后暂停。

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

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

我以为我需要使用ng-change或类似输入区域的内容。我是Angular的新手。

javascript jquery angularjs
2个回答
2
投票

找出答案。您可以在3000是要等待的毫秒数的情况下执行此操作。

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

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>
© www.soinside.com 2019 - 2024. All rights reserved.