I'm using reactive form, I have 2 questions : I want to use a array of strings as an input that the user can change directly, I have a data model like this
export class Work {
toDos: string[];
}
Q1 : Is it ok to bind a input directly to an array of primitives or should I convert toDos in an array of object with a property name, and always use controls?
Q2 : I tried several think, but I can't find a simple way to make it working :
<div *ngFor="let item of toDos;let index = index;trackBy:trackByIndex;">
<input [(ngModel)]="toDos[index]" placeholder="item" name="word{{index}}">
</div>
<button (click)="addItem()" type="button">Add an
Item</button>
<div *ngFor="let item of toDos">
<label>{{item}}</label>
</div>
In the component.ts
toDos: string[] =["Todo1","Todo2","Todo3"];
trackByIndex(index: number, obj: any): any {
return index;
}
addItem() {
this.toDos.push('0');
}
The add function works ok but the input is not bind and I don't get any errors, I can change the value of the input but it's not reflect on {{item}}