But it’s not perfect yet. If I fill a number in the first column, it displays the number in the first and second row and the cursor jumps to the second row.
The index seems to be the problem. But without the index I don’t know which one of the points has been changed.
How can I deal with this without any index?
I’m having a hard time reconciling these two statements. If you’re going to save the whole array regardless of which point has changed, why do you care which point changed?
It would be helpful to see the entire template and backing controller, or at least abridged versions of them that can actually be compiled by others in order to replicate your situation.
Absolutely, sadly I’m having a hard time too
How can I save the updated array?
This one works:
<ion-row *ngFor="let player of players">
<ion-col *ngFor="let points of player">
<ion-input (ngModel)]="points" (ngModelChange)="save()"></ion-input>
</ion-col>
</ion-row>
But if I change a value somewhere, then not the array is edited. Only the value in the field has changed and the ‘points’-model there. But the players array is like before, so I can not save it then, because if I load it again the changes are gone.
I would make Player a proper interface, so that the name is separated out from the scores, instead of mashing everything together into a single array. I would also prefer a separate button for saving, instead of saving the entire shebang on every single little edit, but I assume you have your reasons for wanting to do things this way. Finally, I would not use localStorage. Ionic Storage is better in every situation. localStorage can get erased out from under you with no warning.
All that being said, your primary issue is yet another body on the giant pyre of “things I hate about JavaScript”. When trying to do two-way binding on a nested object, you need to make sure you’re not operating on a transient clone:
<ion-list *ngFor="let player of players; let i = index">
<ion-item-divider>{{player.name}}</ion-item-divider>
<ion-item *ngFor="let frame of player.frames; let j = index">
<!-- transient clone, won't do what you want -->
<ion-input [(ngModel)]="frame" (ionChange)="save()"></ion-input>
<!-- real mccoy, is what you want -->
<ion-input [(ngModel)]="players[i].frames[j]" (ionChange)="save()"></ion-input>
</ion-item>
</ion-list>
Your example code stills breaks with the index on my environment.
But your absolutely right: I’ll do the things in a better way so I’ll don’t need the index.
Thanks for trying to sort this out!
(In ionic 3 it worked I think …)
Define “break”. Obviously you only want the second <ion-input> in there, but I tested it and changes made in the <ion-input> percolate as desired in the save() method.