【问题标题】:Object is modified but the array is not对象已修改,但数组未修改
【发布时间】:2018-07-02 13:30:01
【问题描述】:

我有一个显示在视图中的对象数组,我想使用 http get 调用的结果更新数组中的一个对象。

对象(项目)已更新,但是当我调试它时,似乎对象中的这种更改不存在于数组(this.myArray)中,因此视图中没有任何更改。

component.ts

myFunction() {
        this.myArray.forEach(item => {
                this._appSvc.modifyObject(item).subscribe(result => {
                    item = result;
                });
        });
    }

appSvs.ts

modifyObject(item): Observable<IItem>{
        return this._http.get(url).map(response => <IItem> response.json());
    }

component.html

<tbody>
    <tr *ngFor="let item of myArray | orderBy:{property: sortingAttribute, direction: direction}">
        <td>
            <input type="checkbox" name="select-{{item.id}}"
                   id="select-{{item.id}}"
                   [checked]="item.selected"
                   (change)="selectItem(item.id)"/>
        </td>
        <td>
            <span>{{item.name}}</span>
        </td>

        <td>
            <span>{{item.code}}</span>
        </td>
    </tr>
</tbody>

【问题讨论】:

  • 如何将数组与视图绑定?你喜欢 ngOnChanges 吗?
  • @Mium 检查编辑版本!我使用(更改)指令,但用于对象(即数组的元素)

标签: angular typescript angular5


【解决方案1】:

根据索引和您从服务中获得的新项目修改您的基本数组。

myFunction() {
        this.myArray.forEach((item,index) => {
                this._appSvc.modifyObject(item).subscribe(result => {
                    this.myArray[index] = result;
                });
        });
    }

【讨论】:

  • 你说得对,我没有意识到他没有使用 Observables。我用正确的答案编辑。
猜你喜欢
  • 2012-08-18
  • 2020-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多