Use two ngModel with the same name

0

hi I'm doing a form using angular which contains the following data

<form >

  <input type="text" placeholder="Nombres: "
     [(ngModel)]="nombres" [ngModelOptions]="{standalone: true}" name="nombres"><br>

  <input type="text" placeholder="Apellidos: "
     [(ngModel)]="apellidos" [ngModelOptions]="{standalone: true}" name="apellidos"><br>

  <input type="email" placeholder=" Correo: "
     [(ngModel)]="correo" [ngModelOptions]="{standalone: true}" name="correo"><br>

  <label> Necesitas Servico (de mesero) </label> <br>

  <mat-radio-button [(ngModel)]="servicio" [ngModelOptions]="{standalone: true}"
    name="servicio" value="si"
    (ngModelChange)="servicio= $event">Si</mat-radio-button>

  <mat-radio-button [(ngModel)]="servicio" [ngModelOptions]="{standalone: true}"
    name="servicio" value="no"
    (ngModelChange)="servicio= $event">No</mat-radio-button><br><br>

  <button class="btn-guardar" type="submit" (click)="enviarmail()">Enviar</button>

</form>

I have two ngModel with the same name, when I send the data with the sendmail () function, the service field is sent as undefined.

How can I solve this.

Thanks

    
asked by Gerardo Gutiérrez 20.08.2018 в 17:06
source

1 answer

0

You would have to group the radio buttons in a radio group and define the ngmodel in the group , something like this:

<mat-radio-group
  [(ngModel)]="servicio"
  [ngModelOptions]="{standalone: true}"
  (ngModelChange)="servicio=$event"
  name="servicio">

  <mat-radio-button value="si">Si</mat-radio-button>
  <mat-radio-button value="no">No</mat-radio-button>
</mat-radio-group>
    
answered by 20.08.2018 в 17:44