Javascript 绑定mat select表单中的选定值以传递给submit按钮函数

Javascript 绑定mat select表单中的选定值以传递给submit按钮函数,javascript,html,angular,material-design,formbuilder,Javascript,Html,Angular,Material Design,Formbuilder,根据德国人的回答进行更新 我试图将instrumentName字段从用户选择的仪器值传递到submit按钮中的函数。instrumentName值正确显示,但在提交时似乎从未保存。所有其他输入值保存良好。我怎样才能解决这个问题 HTML: 您不需要通过模板传递Machine\u ID引用。 您可以调用formGroup并获取Machine\u IDformControl值,如下所示: this.createForm.get('Machine_ID').value 因此,在addSample中,您

根据德国人的回答进行更新

我试图将instrumentName字段从用户选择的仪器值传递到submit按钮中的函数。instrumentName值正确显示,但在提交时似乎从未保存。所有其他输入值保存良好。我怎样才能解决这个问题

HTML:


您不需要通过模板传递Machine\u ID引用。 您可以调用formGroup并获取
Machine\u ID
formControl值,如下所示:

this.createForm.get('Machine_ID').value

因此,在
addSample
中,您可以执行以下操作:

addSample(/*other inputs*/) {
  const machineIdValue = this.createForm.get('Machine_ID').value;
  this.fileService.addSample(/*other inputs*/, machineIdValue).subscribe(() => {
    this.router.navigate(['/instruments']);
  });
}
然后,您应该更新“提交”按钮:

<form [formGroup]="createForm" class="create-form">
    <mat-form-field class="field-full-width">
            <mat-label>Instrument</mat-label>
            <mat-select matInput formControlName="createForm.get('Machine_ID')" #Machine_ID>
                <mat-option *ngFor="let instrument of instruments" [value]="instrument.instrumentName">
                    {{instrument.instrumentName}}
                  </mat-option>
            </mat-select>
        </mat-form-field>
</form>
<button (click)="addSample()"
          [disabled]="createForm.invalid" mat-raised-button color="primary">Save</button>

仪器
{{instrument.instrumentName}
拯救

当您单击Submit按钮时,您正在调用什么函数?抱歉,我忘了添加它,它会在单击时将数据发送到“addSample(Machine_ID)”函数。当我执行此操作并对html和ts代码中的addSample参数进行适当调整时,“提交”按钮不再起作用。我更新了答案并更改了提交按钮是的,这就是我更新“提交”按钮的方式,但它现在什么都不起作用尝试删除
mat升起的按钮
property我想我发现了错误,是mat选择中的formControlName。要引用formControl'Machine\u ID',您必须执行以下操作:
formControlName=createForm.get('Machine\u ID')
。在那里我更新了我的答案
addSample(/*other inputs*/) {
  const machineIdValue = this.createForm.get('Machine_ID').value;
  this.fileService.addSample(/*other inputs*/, machineIdValue).subscribe(() => {
    this.router.navigate(['/instruments']);
  });
}
<form [formGroup]="createForm" class="create-form">
    <mat-form-field class="field-full-width">
            <mat-label>Instrument</mat-label>
            <mat-select matInput formControlName="createForm.get('Machine_ID')" #Machine_ID>
                <mat-option *ngFor="let instrument of instruments" [value]="instrument.instrumentName">
                    {{instrument.instrumentName}}
                  </mat-option>
            </mat-select>
        </mat-form-field>
</form>
<button (click)="addSample()"
          [disabled]="createForm.invalid" mat-raised-button color="primary">Save</button>