gpt4 book ai didi

Angular 区分 keydown.enter for textarea in mobile vs desktop

转载 作者:太空狗 更新时间:2023-10-29 17:55:30 24 4
gpt4 key购买 nike

我目前正在开发一个聊天平台,其中有一个使用 textarea 的消息输入框(用户在其中键入他/她的消息)。按 Enter 键允许用户发送消息。但是,我想知道是否可以区分桌面 View 和移动 View 的 Enter 键行为,就像这样 -

如果用户在文本区域中按下回车键:
1.桌面 View :用户可以发送消息
2.移动 View :在textarea字段中创建一个新行,不发送消息

message.component.html

<textarea autosize [minRows]="1" [maxRows]="10" [(ngModel)]="messageToSend"
(keydown.enter)="sendMessage($event)" placeholder="Type a message"></textarea>

message.component.ts

sendMessage(event: KeyboardEvent) {
const message: Message = {
id: this.id,
date: new Date(),
body: this.message,
};
this.chatService.sendChatMessage(id, message).subscribe();
}

感谢您的帮助!

最佳答案

如果你需要检查屏幕尺寸那么你可以尝试matchMedia

if (window.matchMedia('screen and (max-width: 768px)').matches) {
console.log('Screen is less then 768')
}

关于 Angular 区分 keydown.enter for textarea in mobile vs desktop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57847674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com