0

Tried to update object values but not working. Is it possible or not to update object values. How to find a solution for this.If anyone knows please help.

updateFn() {
var elements: any = document.getElementsByTagName("input");
for (var i = 0; i < elements.length; i++) {
  if (elements[i].type == "text") {
    this.selVal[i].key = elements[i].value;
    console.log(elements[i].value);
  }
}
}

Demo: https://stackblitz.com/edit/angular-gss26x?file=src%2Fapp%2Fapp.component.ts

1 Answer 1

1

I think this is what you tried to do, here is example on stackblitz

On HTML:

{{selValues | json}}
<form #form="ngForm">
  <table class="auto" width="100%">
    <thead>
      <tr>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let item of selValues | keyvalue">
        <td>{{item.key}}</td>
        <td><input type="text" [name]="item.key" [ngModel]="item.value"></td>
      </tr>
    </tbody>
  </table>
</form>

<button (click)="resetFn()">Reset</button>
<button (click)="updateFn(form)">Update</button>

On ts file:

  selValues: any;

  originalValues: any = {
    name: "Test",
    gender: "Male",
    id: 1
  };

  ngOnInit() {
    this.resetFn();
  }

  resetFn() {
    this.selValues = { ...this.originalValues };
  }

  updateFn(form: NgForm) {
    this.selValues = form.value;
  }
}

** Because you're using keyvalue pipe you can't use ngModel as two way binding. Check this link.

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

1 Comment

Your concept is not working : stackblitz.com/edit/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.