How to enable shift carriage return + return

I use a text box in a simple chat application. I would like to enable carriage return only when the user presses shift + return and passes the form when the user only clicks on the return key.

I am using the ngKeydown directive. The problem is that when I press enter, the message is sent normally, the text field is cleared, and then the si carriage return is inserted. This means that the event continues to spread ...

Here is the HTML code:

<textarea ng-model="messageTyped" ng-keydown="checkReturn($event)"></textarea> 

and here is the corner symbol code:

 function chatController($scope, ChatService) { $scope.messages = ChatService.messages $scope.checkReturn = function(event) { if (event.keyCode == 13 && !event.shiftKey) { event.stopPropagation() // <------ this line seems to have no effect // submit the message: if ($scope.messageTyped) { ChatService.SendMessage($scope.messageTyped) $scope.messageTyped = "" } } } } 

I don’t know what I am missing ... Thanks for your help!

+7
angularjs
source share
2 answers

I would suggest writing a special directive for this. Then you can use it for any items you need. The following is an example of using it to move to the next input element. Just type enter-next into any element you want to use.

 .directive('enterNext', function() { return { restrict: 'A', link: function($scope,elem,attrs) { elem.bind('keydown', function(e) { var code = e.keyCode || e.which; if ((code === 13) && (e.shiftKey)) { e.preventDefault(); elem.nextAll('input').first().focus(); } }); } } }); 
+5
source share

this can be done with ng-keyup and without any directive:

 <textarea ng-keyup="$event.keyCode === 13 && (!$event.shiftKey) && TARGET_FUNCTION()"></textarea> 
0
source share

All Articles