在指令中设置 formNameGroup 值

Set formNameGroup value inside directive

我使用的是响应式表单。在我的指令中,由于`ElementRef.

,我可以访问该元素

然而,当我尝试使用 this.element.nativeElement.value = 'new text' 设置我的 textarea 元素的值时,该值不会更新并且 formGroupName 不会触发任何 changeValues.

如何正确设置使用 formGroupName 的文本区域元素的值?

以这种方式更改值时,您仍然需要调度事件。它不会自动发送。这就是指令知道值已更改的方式;通过监听输入事件。

this.element.nativeElement.value = 'new text'
this.element.nativeElement.dispatchEvent(new Event('input'));