Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 make textarea press enter without new line added

I have a textarea for some text input. When user finish typing, I let them use [Enter] to confirm the text by

<textarea #msgInput (keyup.enter)="confirmText(msgInput.value)" >
</textarea>

Although i can successfully get the text inside confirmText(). A newline is created on the value of the textarea.

How do I drop the new-line char to the textarea correctly? I know there exist some method like return 0 and preventDefault() when writing js but I have no idea how to do it in Angular2 typescript.

like image 650
Nick Avatar asked Apr 24 '16 11:04

Nick


1 Answers

Add a ;false to the expression to suppress default behavior:

<textarea #msgInput (keydown.enter)="confirmText(msgInput.value);false" >

You also have to use keydown instead of keyup because cancelling on keyup is too late.

Plunker example

like image 196
Günter Zöchbauer Avatar answered Nov 15 '22 14:11

Günter Zöchbauer