我使用的是响应式表单验证(模型驱动验证),但无法在下拉列表更改时将值设置为表单对象
这是我的表单组
studentModel:StudenModel
AMform: FormGroup;
Name = new FormControl("", Validators.required);
Address = new FormControl("", Validators.maxLength(16));
constructor(fb: FormBuilder){
this.AMform = fb.group({
"Name": this.Code,
"Address": this.Abbrev,
});
}
onAccntChange(event: Event) {
// set the value from Class Model
//// this.studentModel
// how to set this.studentModel value to form
}
这是我的 html 页面
<form [formGroup]="AMform" (ngSubmit)="submit()">
<select (change)="onAccntChange($event)" class="form-control" [disabled]="ddlActivity" formControlName="AccountManagerID">
<option value="0">Select</option>
<option *ngFor="let item of allStudent" value={{item.StudentID}}>
{{item.Name}}
</option>
</select>
<div class="col-sm-9">
<input type="text" class="form-control" formControlName="Name">
</div>
<div [hidden]="Name.valid || Code.pristine" class="error"> Name is required </div>
<div class="col-sm-9">
<input type="text" class="form-control" formControlName="Address">
</div>
<div [hidden]="Address.valid || Address.pristine" class="error">Address is required </div>
<button type="submit" class="btn btn-warning "><i class="fa fa-check-square"></i> Save</button>
</form>
在更改时我需要设置 formcontrol 的值
最佳答案
您可以通过在您的 FormControl
对象上调用 setValue
方法来实现:
(<FormControl> this.AMform.controls['Name']).setValue("new value");
或:
this.Name.setValue("new value");
关于javascript - 如何在 Angular 2 Typescript 中为 FormBuilder 对象设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40081065/