0

I have a model that has array inside an array of object:

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

In my component I have:

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

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

When I run this, it doesn't splice and remove the row.
How can i fix it?

4
  • Is this code <button select(n, i)>select</button> working? I guess you missed (click)="..." binding there Commented Jan 14, 2019 at 8:49
  • Sorry it was a type I corrected it. Code is functioning fine just not deleting the row in splice. Thanks Commented Jan 14, 2019 at 8:57
  • can you add a console.log(this.shops.locations[this.indexi].locationssub,length) after splice? Commented Jan 14, 2019 at 9:07
  • I did and there nothing. There is no error. Also entry is not deleted. Issue is nested arrays and index seems like Commented Jan 14, 2019 at 10:03

1 Answer 1

1

Hello there are two possible reasons for this.

  1. Your code does not work (is the entry deleted from your array)
  2. Angular does not regonize the changes of your object, so you have to call changedetection after deleting: https://angular.io/api/core/ChangeDetectorRef
Sign up to request clarification or add additional context in comments.

3 Comments

are you sure? why angular does not recognize?
Code works but entry is not deleted. There is no error either
Could you please provide an example which produces the same error? You can use stackblitz.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.