Angular js输入框输入并暂停

时间:2022-10-18 03:40:06

Am new to angular js and using xmpp service with angular js,I have a situation where i need to send typing when user starts typing in input box and pause when user stops typing.

我是角度js的新手,并且使用带角度js的xmpp服务,我有一种情况,我需要在用户开始输入输入框时发送输入,并在用户停止输入时暂停。

I have basic idea that i need to use $timeout,and have one variable like vm.isTyping = false;

我有基本的想法,我需要使用$ timeout,并有一个变量,如vm.isTyping = false;

i also have written some sort of platform for the same.

我也为此写了一些平台。

vm.isTyping = false;
vm.checkTyping = function(){
    $timeout(function() {
        vm.isTyping = true;
    },2000);

    if(!vm.isTyping){
        vm.sendTyping();
    } else{
        //
    }
};

This is the input field code:

这是输入字段代码:

<input type="text" placeHolder="Type a message here" 
    ng-model="vm.newMessage" 
    ng-blur="focusRemove(vm.newMessage,vm.contact)" 
    ng-change="vm.checkTyping()"
    ng-model-options="{'debounce': 500}" 
    ng-keydown="vm.onKeyDown($event)" auto-focus />

The problem i am facing is that i cannot send typing everytime when user presses any key,one typing is sent on first key press then i should send pause when user stop typing.

我面临的问题是,每当用户按任意键时,我都无法发送打字,在第一次按键时发送一个打字然后我应该在用户停止输入时发送暂停。

Can any one help me to implement code in angular js to achieve this.

任何人都可以帮我实现角度js中的代码来实现这一点。

1 个解决方案

#1


1  

What you can try is have ng-keyup attribute included in you input tag. So when the user starts typing you set the flag vm.isTyping = true. When the user stops typing have a timeout in your ng-keyup handler, that will set vm.isTyping = false after a certain period. Refer the below code snipet

您可以尝试的是输入标记中包含ng-keyup属性。因此,当用户开始输入时,您可以设置标志vm.isTyping = true。当用户停止键入时,ng-keyup处理程序中有超时,这将在一段时间后设置vm.isTyping = false。请参考下面的代码snipet

function myappCtrl($scope, $timeout) {
    var timoutPromise = null;
		$scope.newMessage = "";
    $scope.isTyping = false;
    
    $scope.checkTyping = function() {
    	$scope.isTyping = true;
      if(timoutPromise) {
          $timeout.cancel(timoutPromise);
      }
    }
    
    $scope.stoppedTyping = function() {
    	timoutPromise = $timeout(function() {
        $scope.isTyping = false;
      }, 2000)
    	
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
 <div ng-controller="myappCtrl">
  <input  type="text" placeHolder="Type a message here" ng-model="newMessage" ng-change="checkTyping()"  ng-keyup="stoppedTyping()"/>
  <div ng-if="isTyping">
    typing
  </div>
</div>
</div>

#1


1  

What you can try is have ng-keyup attribute included in you input tag. So when the user starts typing you set the flag vm.isTyping = true. When the user stops typing have a timeout in your ng-keyup handler, that will set vm.isTyping = false after a certain period. Refer the below code snipet

您可以尝试的是输入标记中包含ng-keyup属性。因此,当用户开始输入时,您可以设置标志vm.isTyping = true。当用户停止键入时,ng-keyup处理程序中有超时,这将在一段时间后设置vm.isTyping = false。请参考下面的代码snipet

function myappCtrl($scope, $timeout) {
    var timoutPromise = null;
		$scope.newMessage = "";
    $scope.isTyping = false;
    
    $scope.checkTyping = function() {
    	$scope.isTyping = true;
      if(timoutPromise) {
          $timeout.cancel(timoutPromise);
      }
    }
    
    $scope.stoppedTyping = function() {
    	timoutPromise = $timeout(function() {
        $scope.isTyping = false;
      }, 2000)
    	
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
 <div ng-controller="myappCtrl">
  <input  type="text" placeHolder="Type a message here" ng-model="newMessage" ng-change="checkTyping()"  ng-keyup="stoppedTyping()"/>
  <div ng-if="isTyping">
    typing
  </div>
</div>
</div>