angular 5 dropdown retain values


I am tying to generate some fields on the click of a button. The fields are generated fine but the dropdown in the generated fields are not retaining the values selected. They are pushed to the array but the values are not shown in the form. The code is as follows.


<form #formRef="ngForm">
  <div *ngFor="let test of mytest; let in=index" class="col-sm-3">
      <div class="form-group"  >
        <select  name="fe[in]" class="form-control" [(ngModel)]="" >
          <option  *ngFor="let f of fields" [value]="f">{{f}}</option>
        <select name="ty[in]" class="form-control" [(ngModel)]="test.type"> 
          <option  *ngFor="let ty of types" [value]="ty">{{ty}}</option>
        <input type="text" [(ngModel)]="test.placeholder" name="name{{in}}" class="form-control" #name="ngModel" required />
        <input type="text" [(ngModel)]="test.values" name="name{{in}}" class="form-control" #name="ngModel" />
      <br />
  <button [disabled]="!formRef.form.valid" (click)="add([in])">Add input</button>
<br />
<br />
{{ mytest | json}}


import { Component } from "@angular/core";
import { test } from './test';

  selector: "app-root",
  templateUrl: './app.component.html'
export class AppComponent {
  mytest: test[] = [];
  fields: string[] = ['First Name','Last Name','Email Address','Address1 ','Address2 ','City','Postal Code','Province']
  types: string[] = ['text','email','password','dropdown','radio button','check boxes'];

  add(val) {

    this.mytest.push({name: '',type: '',placeholder:'',values:''});



Has to do with the control name:

<select name="ty[in]"

change this to: e.g.

<select [name]="'f' + in"


Answered By – Marcel Hoekstra

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.