0

I'm trying to bind and use ngModel with a JSON object that is double nested. This is what I'm trying to use: (Calendar from PrimeNG)

<div *ngFor="let field of fields;">
    <div *ngFor="let player of field.players; let i = index;">
        PlayerID: {{player.playerid}}
        <div *ngFor="let goal of player.goals; let j = index;">
            {{goal.timestamp}} <p-calendar [showTime]="true" dateFormat="yy-mm-dd" timeFormat="hh:mm" required [(ngModel)]="field.players[i].goals[j].timestamp"></p-calendar></span><br>
        </div>
    </div>
</div>

However, only the first date value in the array is binded correctly, the others do not work at all.

1 Answer 1

1

Try to use index for the fields as well.

<div *ngFor="let field of fields; let fi = index;">
    <div *ngFor="let player of field.players; let i = index;">
        PlayerID: {{player.playerid}}
        <div *ngFor="let goal of player.goals; let j = index;">
            {{goal.timestamp}} <p-calendar [showTime]="true" dateFormat="yy-mm-dd" timeFormat="hh:mm" required [(ngModel)]="fields[fi].players[i].goals[j].timestamp"></p-calendar><br>
        </div>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

Still appears to produce the same issue.
I just tried it on stackblitz.com/edit/angular-dbjdyw and it worked. I noticed that you added </span> without opening <span>. Is it a typo?
It may be that this markup is inside a form tag, and that each input field must have a different name.
I appreciate the help. It was a missing index and I had a goofy typo.

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.