1

I want to create a solution to update observable array objects on input change

I am creating a table from an array using ngFor from angular having one text field with bid value.

What I want is whenever user updated text field, object consist of that bid value should be automatically updated

Here is my code

Template file:

<table class="table table-striped applist-table table-bordered">
  <thead>
    <tr>
      <th width="10%" class="text-center">App Id</th>
      <th width="40%">App/Site Name</th>
      <th width="30%">Exchange Name</th>
      <th width="20%">Actions</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let a of apps | async">
      <td width="15%" class="text-center">{{ a.sid }}</td>
      <td width="40%">{{ a.appName }}</td>
      <td width="30%">{{ a.exchange }}</td>
      <td width="15%">
        <div class="input-group">
          <input type="text" class="form-control" value="{{a.dynamic_bid}}" #bid />
          <div class="input-group-append">

          </div>
        </div>
      </td>
    </tr>
  </tbody>
</table>

component.ts

apps: Apps[] = [];
openBidUpdateOptions(content) {
  this.loading = true;

  this._campaignOperations.loadCampaignApplistData(this.id).subscribe(
    data => {
      if (data.status == 1200) {
        this.loading = false;
        this.apps = data.data;
        this.modalReference = this.modalService.open(content, {
          size: 'lg'
        });
        this.modalReference.result.then((result) => {

        }, (reason) => {

        });

        this.loading = false;
      } else {

        let str: any = '';
        let errorList = data.errors;
        for (let i in errorList) {
          str += errorList[i] + "<br>";
        }

        this.toastr.error(str, 'Error');
        this.loading = false;
        return false;
      }

    },
    error => {
      swal({
        position: 'center',
        type: 'error',
        title: 'Unable to create campaign due to unknown error',
        showConfirmButton: false,
        timer: 1500
      });
      this.loading = false;
      return false;
    });
}

export interface Apps {
  id ? : string;
  sid ? : string;
  appName ? : string;
  exchange ? : string;
  dynamic_bid ? : string;
}

Here is demo for same : https://stackblitz.com/edit/angular-kmjrqd

5
  • You mean you want to get values from back-end and update? Commented Nov 22, 2018 at 10:00
  • Nope i already have array from retrieved from backend i want to update specific object on input change Commented Nov 22, 2018 at 10:03
  • Can you update your example in stackblitz so the problem can be more clear Commented Nov 22, 2018 at 10:22
  • sure here it is, stackblitz.com/edit/angular-kmjrqd whenever textbox updates i want object in array should be updated Commented Nov 22, 2018 at 10:37
  • 1
    as @bluray suggests to make it [(ngModel)] this is the robust solution and will make your task very will Commented Nov 22, 2018 at 11:31

1 Answer 1

2

I dont sure if I understand your problem. But I think you can use ngModel:

  <input type="text" class="form-control" [(ngModel)]="a.dynamic_bid" #bid />
Sign up to request clarification or add additional context in comments.

4 Comments

Nope, this wont work, here i will have multiple rows with each having text field consist of bid value. i don't want to write any click on input change event. whenever input value updates i want that specific property to be updated on same array. i can make this possible by other ways as well but don't want to make more complex
So what is wrong in this answer. you have 2 way databinding and your array will be updated?
Thanks, sorry i was not aware of solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.