首页 > 解决方案 > 删除嵌套数组Angular 2/4中的对象行

问题描述

我有一个模型,它在对象数组中包含数组:

<div *ngFor="let n of shops?.locations let i=index;">
   <span>{{n.name}}</span>
   <button (click)="select(n, i)">select</button>
</div>
<popup>
<div *ngFor="let subloc of locationssub let j=index;">
   <span>{{subloc.id}}</span>
   <span><{{subloc.name}}</span>
   <button  (click)="delete(subloc, j)">del</button>
</div
</popup>

在我的组件中,我有:

 select(n, i){
    this.indexi=i;
    this.locationssub=n;
    this.popup.show();
 }

 delete(subloc, j){
    this.shops.locations[this.indexi].locationssub.splice(j,1);
 }

当我运行它时,它不会拼接和删除该行。
我该如何解决?

标签: javascripthtmlangulartypescript

解决方案


您好,有两个可能的原因。

  1. 您的代码不起作用(是从您的数组中删除的条目)
  2. Angular 不会重新调整对象的更改,因此您必须在删除后调用 changedetection:https ://angular.io/api/core/ChangeDetectorRef

推荐阅读