基本上我正在尝试编辑和更新我在数据库上的现有数据。 因此,首先根据需要,我从DB获取特定Employee的数据并将其设置为我的EMPLOYEE属性,如下所示 - ,
这是我从DB获取数据后保存数据的类属性。
employee: any; //This is where the employee saved after getting from DB
此方法从数据库获取数据并成功。
getEmployee(id){
var url = `http://localhost:3000/employee/get/${id}`;
this.crudService.getItem(url).subscribe((data:any)=>{
this.employee = data.data;
})
}
之后我想使用并通过Class Property EMPLOYEE将值设置为我的一个Form输入作为默认值。我正在使用它(如下)和FORM CONTROL,但没有工作 - ,
this.employeeName = new FormControl(
this.employee.employeeName, [Validators.required, Validators.minLength(5), Validators.maxLength(50)]
);
但是当我通过VALUE ATTRIBUTE直接将它用于html时它会起作用,如下所示 - ,
<input type="text" class="form-control form-control-sm"
id="inputEmployeeName"
name="employeeName"
formControlName="employeeName"
value="{{employee.employeeName}}"
>
但我不想要这个。我想将值设置为FORM CONTROL。 我怎样才能做到这一点 ?
答案 0 :(得分:2)
尝试以下代码
this.myFormGroup.setValue({
formControlName1: myValue1,
formControlName2: myValue2
});