0

In Angular How to Load the radio button value when i click on edit form button.

This is my UI.

enter image description here

when i click on save the data is inserted into the data Table. But when i click on edit button the form is not loading with the radio button values and when i click on Update button the data should be affected into the Data table.

app.component.html

<div class="col-sm-10"> 
     <input type="radio" name="gender" [(ngModel)]="model.gender" value="male" checked> Male                         
     <input type="radio" name="gender" [(ngModel)]="model.gender" value="female"> Female<br>
 </div>
<button type="submit" class="btn btn-default" (click)="addEmployee()">Save</button>
<a class="btn btn-success" (click)="editEmployee(i)">Edit</a>
<button type="submit" class="btn btn-default" (click)="updateEmployee()">Update</button>

this is my app.component.ts

myValue;
  editEmployee(k){
    this.model2.name = this.employees[k].name;
    this.model2.DOB=this.employees[k].DOB;
    this.model2.mob = this.employees[k].mob;
    this.myValue = k;
  }

  updateEmployee(){
    let k= this.myValue;
    for(let i=0; i<this.employees.length;i++){
      if(i==k){
        this.employees[i]= this.model2;
        this.model2 = {};   
        this.msg = "Record is successfully updated..... ";
      }
    }  
  }
1
  • Use model.gender = this.employees[k].gender in EDIT method. If not, Please refer this url embed.plnkr.co/plunk/quf8T5 Commented Oct 22, 2018 at 11:07

2 Answers 2

0

The data binding with radio buttons work with the value attribute and is not dependent of the checked attribute. So if you set e.g. "male" as default in your model it will be selected in the template.

So to load the gender just access model.gender which is either "male" or "female". And to update Angulars Two-way databading should do it for you whenever you click on one of the radiobuttons, because it will update your model automatically.

https://stackblitz.com/edit/angular-eferut?file=src%2Fapp%2Fapp.component.html

Sign up to request clarification or add additional context in comments.

Comments

0

Your html is looking fine you need to make the change in ts file. Basically name model12 is wrong and you don't need to assign the single value separately.

Your code will look like -

  model;
  selectedIndex;

  editEmployee(k){
    this.selectedIndex = k;
    this.model = Object.assign({},this.employees[k]);
  }

  updateEmployee(){

    if(this.employees[this.selectedIndex]){
      Object.assign(this.employees[this.selectedIndex], this.model);
       this.msg = "Record is successfully updated..... ";
    }else{
      this.employees.push(Object.assign({}, this.model));
      this.msg = "Record is successfully added..... ";
    }
  }

1 Comment

please check my post i have updated it. Seriously I'm not getting how to perform that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.