【问题标题】:How to update array on input change using async in angular 5?如何使用角度 5 中的异步更新输入更改数组?
【发布时间】:2018-11-22 09:30:38
【问题描述】:

我想创建一个解决方案来在输入更改时更新可观察数组对象

我正在使用 ngFor 从一个数组创建一个表格,该表格具有一个带有投标值的文本字段。

我想要的是每当用户更新文本字段时,包含该出价值的对象应该自动更新

这是我的代码

模板文件:

<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>

组件.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;
}

这是相同的演示:https://stackblitz.com/edit/angular-kmjrqd

【问题讨论】:

  • 你的意思是你想从后端获取值并更新?
  • 不,我已经从后端检索到数组,我想在输入更改时更新特定对象
  • 您能否在 stackblitz 中更新您的示例,以便更清楚问题
  • 确定在这里,stackblitz.com/edit/angular-kmjrqd 每当文本框更新我希望数组中的对象应该被更新
  • 正如@bluray 建议的那样 [(ngModel)] 这是一个强大的解决方案,将使您的任务非常顺利

标签: javascript angular rxjs observable


【解决方案1】:

我不确定我是否理解您的问题。但我认为你可以使用 ngModel:

  <input type="text" class="form-control" [(ngModel)]="a.dynamic_bid" #bid />

【讨论】:

  • 不,这行不通,这里我会有多行,每行的文本字段都包含出价值。我不想写任何点击输入更改事件。每当输入值更新时,我希望在同一个数组上更新该特定属性。我也可以通过其他方式实现这一点,但不想变得更复杂
  • 那么这个答案有什么问题。你有 2 路数据绑定,你的数组将被更新?
  • 谢谢,抱歉我不知道解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多