Angular 6 Responsive Forms-如何基于对另一个输入的选择填充一个文本字段
我不熟悉Angular 6和reactive forms,我正在尝试选择一个字段来自动填充另一个字段 目标:如果从“选择”菜单中选择数字“1”,系数输入将自动填充到“.15”。选择“2”将等于“.175”,选择“3”将等于“.2” 实现这一目标的最佳方法是什么?非常感谢 这是我的表格:Angular 6 Responsive Forms-如何基于对另一个输入的选择填充一个文本字段,angular,typescript,Angular,Typescript,我不熟悉Angular 6和reactive forms,我正在尝试选择一个字段来自动填充另一个字段 目标:如果从“选择”菜单中选择数字“1”,系数输入将自动填充到“.15”。选择“2”将等于“.175”,选择“3”将等于“.2” 实现这一目标的最佳方法是什么?非常感谢 这是我的表格: <form name="form" class="form-horizontal" (ngSubmit)="createForm.form.valid && createNewMonitor
<form name="form" class="form-horizontal" (ngSubmit)="createForm.form.valid && createNewMonitoringPoint()" #createForm="ngForm" novalidate>
<select class="col-md-12 form-control" [(ngModel)]="newmp.number"
[ngClass]="{ 'is-invalid': createForm.submitted && newmp.number.invalid }"
required name="number">
<option value="" disabled selected>Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<p>Pipe Roughness Coefficient (n) </p>
<input type="coefficient" id="coefficient"
#coeffcient="ngModel" name="coefficient [(ngModel)]="newmp.coefficient"
class="form-control" placeholder="e.g. .016" />
<button type="submit" value="Create">Create</button>
</form>
选择。。。
1.
2.
3.
管道粗糙度系数(n)
您使用的是模板表单,而不是被动表单。您只需选择表单上的(更改)
方法,如下所示:
<select (change)="selected()">...</select>
解释:您使用的是双向数据绑定,输入使用[(ngModel)]
。模型中的变化反映了输入值,反之亦然。在中更改数据后,选择change()
事件触发器,并调用方法select()
或任何您命名的方法。您不需要传递值,因为this.newmp.number
保存新值,您可以通过this.newmp.coefficient
执行逻辑并为输入分配值。同样,由于它是双向数据绑定,因此逻辑中的值更改为this.newmp.coefficient
,自动反映HTML模板中输入的值。您使用的是模板表单而不是反应表单。您只需选择表单上的(更改)
方法,如下所示:
<select (change)="selected()">...</select>
解释:您使用的是双向数据绑定,输入使用[(ngModel)]
。模型中的变化反映了输入值,反之亦然。在中更改数据后,选择change()
事件触发器,并调用方法select()
或任何您命名的方法。您不需要传递值,因为this.newmp.number
保存新值,您可以通过this.newmp.coefficient
执行逻辑并为输入分配值。同样,由于它是双向数据绑定,因此逻辑中的值更改为this.newmp.coefficient
,自动反映HTML模板中输入的值