1

Say you have a list of tasks which in turn has a list of subtasks and you want the subtasks to be changable - How come that angular doesn't properly two-way bind the data for the subtask?

HTML

<div *ngFor="let task of tasks">
  Task value: <input [(ngModel)]="task.value">
  <br>
  <div *ngFor="let subtask of task.subtasks">
    Subtask: <input [(ngModel)]="subtask">
    </div>
</div>

{{ tasks | json }}

TS

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  tasks = [{
    value: 'Task 1',
    subtasks: ['Hello']
  }]  
}

https://stackblitz.com/edit/angular-agrzfs

2 Answers 2

2

The problem here is with ngFor Each input has to have a unique name . To solve it,

use task.subtasks[index] instead of item with ngModel

Also you need to use trackByIndex inorder to avoid the slowness as it re-creates the DOM every time you change a string in your array

<div *ngFor="let subtask of task.subtasks;let index = index;trackBy:trackByIndex;">
    Subtask: <input [(ngModel)]="task.subtasks[index]">
</div>

STACKBLITZ DEMO

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

Comments

1

You need to access index of sublist like on this code snippet

add index counter on list and access via tas.sublist[i]

   <div *ngFor="let task of tasks">
      Task value: <input [(ngModel)]="task.value">
    <br>
    <div *ngFor="let subtask of task.subtasks; let i = index">
      Subtask: <input [(ngModel)]="task.subtasks[i]">

   </div>
 </div>

{{ tasks | json }}

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.