1

in normal html we can do that:

<input name="xyz[]" value="Lorem" />
<input name="xyz[]" value="ipsum"  />
<input name="xyz[]" value="dolor" />
<input name="xyz[]" value="sit" />
<input name="xyz[]" value="amet" />

and the result is array sent to the server.
how can i do that in angular 5 ?

it's not working the same way also not working in ngModel.

3
  • 1
    Just because you can, doesn't mean you should ;) What is your goal ? Infinite loop, or you have an array length ? Commented Jan 23, 2018 at 12:31
  • I already have length and I use it in angular like that . [(ngModel)]="xyz[i]" but this isn't work Commented Jan 23, 2018 at 13:02
  • I'm making you an answer, wait a second Commented Jan 23, 2018 at 13:04

3 Answers 3

1

I have never used it, but can't you use an index?

<input type="text" [(ngModel)]="xyz[0]" />
<input type="text" [(ngModel)]="xyz[1]" />
<input type="text" [(ngModel)]="xyz[2]" />
<input type="text" [(ngModel)]="xyz[3]" />

I have seen this in this github's issue.

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

Comments

1

I used angular Form Group and this was the solve for this problem :

<div ngModelGroup="xyz">
    <input name="1" ngModel />
    <input name="2" ngModel />
    <input name="3" ngModel />
</div>

Comments

0

Since you have a length on your array, you can either use reactive forms with a FormArray, or use template-driven forms. Since you stated using the latter one, here is how you should do it :

<ng-container *ngFor="let item of xyz; let i = index; trackBy: myCustomTrackBy">
  <input type="text" [(ngModel)]="xyz[i]">
</ng-container>

In your Typescript :

myCustomTrackBy(index, item) { return index; }

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.