javascript - Angular 和 vanila JS - 设置输入值
问题描述
我想在 Agnular 创建的网站中设置输入值(在 Vanilla JS 中)。
当我尝试这样做时:
document.querySelector('my_input').value = 'value';
该值显示在输入中,但搜索没有看到该值(表单已提交,但看起来我的输入为空)。
我看到有事件 ngModelChange。
有没有办法做这样的事情:
document.querySelector('my_input').dispatchEvent(new Event('ngModelChange', {value: 'value'}));
如果我这样做,我会输入 [object Event] 并且表单搜索会得到这个值。
有任何想法吗?
解决方案
我解决了我的问题。这段代码对我有用:
document.querySelector('my_input').value = 'my_value';
document.querySelector('my_input').dispatchEvent(new Event('input'));
document.querySelector('my_input').dispatchEvent(new Event('blur'));
谢谢你的回答。
推荐阅读
- shell - 在 Jenkins 中使用多个 shell
- javascript - 如何使用 ajax 将客户端 javascript 变量发送到服务器
- python - Django QuerySet 差异方法不起作用
- python - Scrapy无法转到下一页
- codeigniter - codeigniter - 验证激活码后重定向用户
- python - python - 如何在没有身份验证的情况下使用python从telnet获取数据
- angular - 带有 asObservable 的 Subject.error 导致 Angular 6 异步管道快速消耗所有系统内存
- android - 回收站视图未显示内容
- css - Gmail 中的基础电子邮件错误
- bash - 任何工具可以并行化具有不同 CPU 要求的 bash 命令?