我收到以下错误
无法读取未定义的属性“值”
以下是我的代码
<select class="form-control form-control-lg" formControlName="contractor" (ngModelChange)="onChange($event.target.value)" >
<option *ngFor="let contrac of contractors" [ngValue]="contrac">{{contrac.name}}</option>
</select>
<label class="control-label col-lg-2">Agency</label>
<div class="col-lg-10">
<input type="text" class="form-control form-control-lg" placeholder="Payroll Number" formControlName ="selectedAgency">
</div>
ngOnInit() {
this.form = this.fb.group({
agency: ['', Validators.required],
contractor: ['', Validators.required],
selectedAgency : ['', Validators.required]
});
onChange = (contrac) => {
alert(contrac.vendor);
this.form.patchValue({ selectedAgency: contrac.vendor });
}
这里有什么遗漏吗?
-Alan -
答案 0 :(得分:0)
应该是:
(ngModelChange)="onChange($event)"
...或者使用(change)
:
(change)="onChange($event.target.value)"
但是,似乎你只是用它来为表单中的另一个控件设置一个值,对吗?
因此,您可以在要设置值的输入上使用[ngModel]
,如下所示:
<select class="form-control form-control-lg" formControlName="contractor">
<option *ngFor="let contrac of contractors"
[ngValue]="contrac">{{contrac.name}}</option>
</select>
<label class="control-label col-lg-2">Agency</label>
<div class="col-lg-10">
<input type="text"
class="form-control form-control-lg"
placeholder="Payroll Number"
formControlName ="selectedAgency"
[ngModel]="myForm.get('contractor').value.vendor">
</div>