【发布时间】:2022-07-09 21:33:29
【问题描述】:
我想用 ts 方法通过给定名称或 id 值更新输入字段,其 id 或名称用字符串给出。
我尝试使用更新 ngModel ngValue 的不同选项,但仅此而已。
所以我有一个 ipnut 字段
<div class="col-auto">
<input type="text" id="yproduct" name="yproduct" class="form-control" ngModel required>
</div>
我改变它的价值形式服务:
this.stockInfoService.caller.subscribe(
data => {
this.caller = data
document.getElementById(this.caller).value = this.codeScan;
}
);
一切都在 html 端工作,字段值显示在输入中,但是当我提交表单时,我的 yproduct 值为空!
{yproduct: ''}
【问题讨论】:
-
你不应该使用
document对象,Angular 应该是处理 DOM 的对象。另外,您是否尝试过使用Template driven forms或Reactive forms?