I am facing a strange behavoir when using ngFor:
General usage: I have a text list stored on my backend. The user can display this list an gets a list of generated by ngFor with the values preset to the list content. Additionally he can edit the text and send the updated one to the backend. The backend will then update the whole list and sends a notify to all clients. If one of the clients is displaying the updated list it fetches the list again, so that it is up to date. (This happens ofc to the editor in any case, so there is no special threatment to the editor)
Maybe my approach is not the best but the problem is a different one: The new list arrives asynchronous and gets updated. The problem is that the text which was used to edit will be pushed "down". The list is not correctly displayed while the backend has the correct list aswell as the new fetched list arrives correctly on the client. It has to have a display/change detection issue.
I tried with the "track by" feature without success.
Minimal working example:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `<div *ngFor="let i of testList">
<input type="text" [value]="i">
</div>
<button (click)="onClick()">Click</button>`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'test-app';
testList = ["one", "two", "three", "", ""];
onClick()
{
setTimeout(()=>{
this.testList = ["one", "two", "three", "four", ""];
}, 2000)
}
}
Write something in the fourth text field, then press the button. After the 2 seconds your text should be shifted to the 5th field.
Thank you in advance