Sushil
Sushil

Reputation: 2490

angular unable to set value to form control

Basically I'm trying to edit and update my existing data on database. So, First as required, i'm getting data from DB for a particular Employee and set it to my EMPLOYEE property, as shown below --,

This the class property where i'm saving data after getting from DB.

  employee: any; //This is where the employee saved after getting from DB

This method is getting data from DB and it getting successfully.

getEmployee(id){
    var url = `http://localhost:3000/employee/get/${id}`;    
    this.crudService.getItem(url).subscribe((data:any)=>{
      this.employee = data.data;     
    })
  }

after that i want to use and set the value to one of my Form input as default value by the Class Property EMPLOYEE. i'm using it like this(below) with FORM CONTROL, but not working --,

this.employeeName = new FormControl(
      this.employee.employeeName, [Validators.required, Validators.minLength(5), Validators.maxLength(50)]
    );

But it working when i'm using it to the html directly by the VALUE ATTRIBUTE as shown below --,

<input type="text" class="form-control form-control-sm" 
 id="inputEmployeeName" 
 name="employeeName" 
 formControlName="employeeName" 
 value="{{employee.employeeName}}"
 >

but i'm not want this. I want to set the value to the FORM CONTROL. How can i Do that ?

Upvotes: 1

Views: 5159

Answers (1)

Sudhir Sapkal
Sudhir Sapkal

Reputation: 1038

Try something like below code

this.myFormGroup.setValue({
  formControlName1: myValue1, 
  formControlName2: myValue2
});

Upvotes: 3

Related Questions