Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从开发人员控制台填写表单_Javascript_Angular_Angular9 - Fatal编程技术网

Javascript 从开发人员控制台填写表单

Javascript 从开发人员控制台填写表单,javascript,angular,angular9,Javascript,Angular,Angular9,在Angular 8中,我能够做到这一点: const el = document.querySelector("#first-name); if (!el) { console.warn(item.selector, 'not found'); return; } const probe = ng.probe(el); const ref = probe.providerTokens.find(p => p.name === 'NgControl'); const directive =

在Angular 8中,我能够做到这一点:

const el = document.querySelector("#first-name);
if (!el) { console.warn(item.selector, 'not found'); return; }
const probe = ng.probe(el);
const ref = probe.providerTokens.find(p => p.name === 'NgControl');
const directive = probe.injector.get(ref);
directive.control.setValue("Johnnie");
这将找到一个
输入
,其后面有一个
formControlName
和基于反应式表单的代码,设置其值,包括触发角度变化检测

在带常春藤的Angular 9中,这不再起作用,因为
ng.probe
已被移除,取而代之的是其他工具,最明显的是
ng.getComponent
。然而,该方法似乎主要用于获取实际的角度分量,我不确定如何使用该方法编辑形状值并触发更改检测


Angular 9&Ivy是如何做到这一点的?

这对我来说很有效,您只需更改纯JS中的输入值,然后触发更改事件。可以对多个输入使用同一事件

const changeEvent = document.createEvent("HTMLEvents");
changeEvent.initEvent("change", false, true);

const el = document.querySelector("#first-name);
el.value = 3;
el.dispatchEvent(changeEvent);