0

Problem: There is an array e.g.

[{id: 1, name: "stefan"}, {id: 2, name: "alex"}, ...]

This list is bound to a *ngFor which can also show details.

Now there comes an update from the server with a new list

[{id: 1, name: "stefanie"}, {id: 2, name: "alexandra"}, ...]

So the ids are identical, but names are the same.

Requirement: Now if I want to assign the new array to the existing array, *ngFor gets completly refresh, hidding my details window. However I just want to

  • update the item if one with the same id already exists
  • add new items
  • delete items which don't exist anymore

I found answers, how you can make this by filtering and updating, however it looks like a pretty common problem to me. So I guess there is alreay a build in function or a clever library which solves this... right?

2 Answers 2

1

You might be looking for the TrackBy feature of angular. With TrackBy, Angular is able to detect which items are deleted or added to an array, using a unique identifier. So, only the items added or removed have to be updated in the DOM.

Here is an extended explanation of TrackBy.

Sign up to request clarification or add additional context in comments.

Comments

1

You are looking for *ngFor with trackBy see this example.

Comments

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.