首页 > 解决方案 > 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] 并且表单搜索会得到这个值。

有任何想法吗?

标签: javascriptangular

解决方案


我解决了我的问题。这段代码对我有用:

document.querySelector('my_input').value = 'my_value';
document.querySelector('my_input').dispatchEvent(new Event('input'));
document.querySelector('my_input').dispatchEvent(new Event('blur'));

谢谢你的回答。


推荐阅读