专注于Angular以后的输入(模态显示)之后,在Safari iOS中显示键盘
我需要在页面加载后设置焦点和打开键盘,或显示带有输入的模式。
Simple .Focus()在Android中工作,也可以在景观模式下在iPad中工作。 但是,在肖像模式下和iPhone焦点上设置了键盘,但未显示键盘。
我还尝试了解决其他元素并关注其他元素的解决方案,但它与Angular不起作用。 ios show键盘在输入焦点上
( ')searchBarInput:ElementRef;
ngAfterViewInit(): void {
setTimeout(()=> {
this.searchBarInput.nativeElement.focus();
this.searchBarInput.nativeElement.click();
},180);
}
测试应用程序: https://inputfocus.vercel.app/
期望是加载后,用户可以,用户可以,用户可以开始键入。 它是简化的版本 - 我需要在模态上,但是iOS上的行为相似
I need to set focus and open keyboard after page loads or modal with input is shown.
Simple .focus() works in Android and also in iPad in landscape mode.
However in portrait mode and on iPhone focus is set but keyboard not shown.
I tried also solution with adding and focusing on additional element, but it doesn't work with Angular. (IOS show keyboard on input focus)
@ViewChild('searchBarInput') searchBarInput: ElementRef;
ngAfterViewInit(): void {
setTimeout(()=> {
this.searchBarInput.nativeElement.focus();
this.searchBarInput.nativeElement.click();
},180);
}
test application:
https://inputfocus.vercel.app/
expectation is that after page is loaded and focus set, user can start typing.
It is simplified version - I need this on modal, but behaviour on iOS is similar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想我找到了解决方案。
在iOS(iPhone)和iPad肖像模式下,需要通过用户操作触发focus()。
因此,我们需要在使用单击按钮后立即设置此此模式或带有目标输入字段的新DIV。
我们可以自动创建这个新字段,设置焦点并在将重点移至目标字段后将其删除。
在按钮上,我们需要创建临时输入,附加到现有容器(靠近我们的输入)并关注它。
显示模式/目标输入后,我们将焦点移动并删除临时模板:
和模板:
您只需要记住iPhone可能会放大屏幕,因此您需要调整临时输入的参数。
工作解决方案: https://inputfocus.vercel.vercel.app/
I think I found the solution.
on iOS (iphone) and iPad portrait mode, focus() needs to be triggered by user action.
So we need to set this immediately after use clicks button showing modal or new div with target input field.
We can create this new field automatically, set focus, and remove it after moving focus to target field.
On button click we need to create temporary input, append to existing container (close to our input) and focus on it.
after modal/target input is shown, we move focus and remove temporary one:
and template:
You just need to remember that iPhone may zoom screen, so you need to adjust parameters of temporary input.
working solution: https://inputfocus.vercel.app/
基于您的答案,我将代码抽象为指令,因此更清晰,因此无需处理事件:
只需使用它 :在您的输入元素中:
Based on your answer, I abstracted the code into a directive so it's clearer and there's no need to handle events:
Just use it in your input element:
您第一次需要这样创建这样的函数,因此可以重复使用
,然后从您想要的任何地方调用该功能
first time you need to create function like this, so can be reusable
and then call the function from wherever you want