user2899728
user2899728

Reputation: 2319

Angular 2: How to create input array?

I am creating template driven form by following this guide: https://angular.io/guide/forms

I need array of inputs e.g.

<input 
  *ngFor="let screenshot of screnshots" 
  [(ngModel)]="screenshot.id" 
  type="text" 
  name="screenshots[]" />

But in {{form.value}}, it is showing just one input value instead of multiple.

I want output of {{form.value}} like this : {screenshots:[1, 2, 3]}

I need this only in the form of array.

is it possible to implement in such way? or what is the best solution to achieve this goal?

Upvotes: 2

Views: 740

Answers (1)

Trash Can
Trash Can

Reputation: 6824

Input type text doesn't support multiple values like input type select or checkbox does, just give each input box a different name like this

<input 
  *ngFor="let screenshot of screnshots;let i = index" 
  [(ngModel)]="screenshot.id" 
  type="text" 
  [name]="'screenshot' + i"

Upvotes: 1

Related Questions