1

I have an input which I can fill dynamically through a request to an API or manually, the problem occurs when I try to send the value of the input, when it has been filled dynamically it sends me an empty value ... I am a beginner in Angular

This is my input HTML

<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="Nombres del Clientes" [(value)]="clienteMkw.datos[0].nombre" name="nombres_cliente" [(ngModel)]="ordenServicio.nombres_cliente">

"clienteMkw" in [(value)] is my object that get the API response

When i try to send the data of the input fill manually on json object it works

{
  nombres_cliente: "ADUCARGAS (AEROPUERTO)"
}

But this is when the inputs are filled dynamically

{
  nombres_cliente: ""​
}

I don't know what am i doing wrong... :s

Here is my TS file

export class CrearOrdenComponent implements OnInit {

  usuarios: any = [];

  tipoOrdenes: any = [];

  ordenServicio: any = {
    idestatus_orden: 1,
    idtipo_orden: 0,
    idusuario: 0,
    nombres_cliente: '',
    telefono_cliente: '',
    movil_cliente: '',
    direccion_cliente: '',
    ppp_user: '',
    ppp_pass: '',
    idtipo_falla: 0,
    fecha_reporte : Date,
    fecha_asig: Date,
    fecha_ejecucion: Date,
    hora_inicio: 0,
    hora_fin: 0,
    diagnostico_inicial: '',
    diagnostico_final: '',
    calificacion: 0
  } 


clienteMkw: any = [];

data: string;
postData: Consulta = {
  token: 'eURGUHVuY2ZSbWkweXNXbVlVT0pZZz09',
  idcliente: 0
};

constructor(
private _servicio: MaterialesService,
private _servicioCliente: ClientesService) { }

ngOnInit() {
  this._servicio.getUsuarios().subscribe(
    res => {
      this.usuarios = res
      console.log(res)
    },
    err => console.error(err)
  );
  this._servicio.getTipoOrden().subscribe(
    res => {
      this.tipoOrdenes = res
    },
    err => console.log(err)
  );
}

sendId(){

  this._servicioCliente.getCliente(this.postData).subscribe(
    data => {
      console.log(this.postData)
      this.clienteMkw = data
      console.log(data)
    },
    err => console.error(err)
  );

}

crearOrden(){
  this._servicio.saveOrden(this.ordenServicio).subscribe(
    res => {
      console.log(res)
      console.log(this.ordenServicio)
    },
    err => console.error(err)
  )
}

}

and my HTML file

<div class="container">

    <div class="card text-left mb-3">
        <form>
            <div class="card-header">
                Obtener Cliente
            </div>
            <div class="card-body">
                <p class="card-text">Ingrese el código Mkw del cliente para obtener sus datos..</p>
                <input type="text" class="form-control" id="exampleFormControlInput1" name="idcliente"
                    [(ngModel)]="postData.idcliente" placeholder="Ej. 1713">
            </div>
            <div class="card-footer">
                <button class="btn btn-primary" (click)="sendId()">Obtener Mkw</button>
            </div>
        </form>
    </div>

    <div class="card text-left">
        <div class="card-header">
            Orden de Servicio
        </div>
        <div class="card-body">
            <form>
                <div class="row my-2">
                    <div class="col-md-4">
                        <label for="calendario">Fecha de Asignación</label>
                        <input class="form-control" id="calendario" type="date" class="type" name="fecha_asig" [(ngModel)]="ordenServicio.fecha_asig">
                        <input class="form-control" id="calendario" type="date" class="type" name="fecha_reporte" [(ngModel)]="ordenServicio.fecha_reporte">
                    </div>
                    <div class="col-md-4">
                        <label for="">Tipo de Orden</label>
                        <select class="form-control" id="" name="idtipo_orden" [(ngModel)]="ordenServicio.idtipo_orden">

                            <option *ngFor="let tipoOrden of tipoOrdenes" [ngValue]="tipoOrden.idtipo_orden">
                                {{tipoOrden.descripcion}}
                            </option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label for="tecnicos">Técnico Asignado</label>
                        <select class="form-control" id="tecnicos" name="idusuario" [(ngModel)]="ordenServicio.idusuario">

                            <option *ngFor="let usuario of usuarios" [ngValue]="usuario.idusuario">                                                     
                                {{usuario.nombres_usuario}}
                            </option>
                        </select>
                    </div>
                </div>
                <div class="row my-2">
                    <div class="col-md-8">
                        <label for="exampleFormControlInput1">Cliente</label>
                        <input type="text" class="form-control" id="exampleFormControlInput1"
                            placeholder="Nombres del Clientes" name="nombres_cliente" [(ngModel)]="clienteMkw.datos[0].nombre">
                    </div>
                    <div class="col-md-4">
                        <label>Estatus</label>
                        <select class="form-control" id="tecnicos">
                            <option>{{clienteMkw.datos[0].estado}}</option>
                            <option>Activo</option>
                            <option>Suspendido</option>
                        </select>
                    </div>
                </div>
                <div class="row my-2">
                    <div class="col-md-12">
                        <label for="exampleFormControlInput1">Dirección</label>
                        <input type="text" class="form-control" placeholder="Dirección"
                            [(ngModel)]="clienteMkw.datos[0].direccion_principal" name="direccion_cliente">
                    </div>
                </div>
                <div class="row my-2">
                    <div class="col-md-4">
                        <label for="">Teléfono 1</label>
                        <input type="text" class="form-control" id="exampleFormControlInput1" placeholder=""
                            [(ngModel)]="clienteMkw.datos[0].telefono" name="telefono_cliente">
                    </div>
                    <div class="col-md-4 offset-md-4">
                        <label for="">PPPoE User</label>
                        <input type="text" class="form-control" id="exampleFormControlInput1" placeholder=""
                        [(ngModel)]="clienteMkw.datos[0].servicios[0].pppuser" name="ppp_user">
                    </div>
                </div>
                <div class="row my-2">
                    <div class="col-md-4">
                        <label for="">Teléfono 2</label>
                        <input type="text" class="form-control" id="exampleFormControlInput1" placeholder=""
                        [(ngModel)]="clienteMkw.datos[0].movil" name="movil_cliente">
                    </div>
                    <div class="col-md-4 offset-md-4">
                        <label for="">PPPoE Password</label>
                        <input type="text" class="form-control" id="exampleFormControlInput1" placeholder=""
                        [(ngModel)]="clienteMkw.datos[0].servicios[0].ppppass" name="ppp_pass">
                    </div>
                </div>
                <div class="row my-3">
                    <label for="te">Requerimiento</label>
                    <textarea class="form-control" name="" id="te" cols="12" name="diagnostico_inicial" [(ngModel)]="ordenServicio.diagnostico_inicial"></textarea>
                </div>
                <div class="card-footer text-muted">
                        <button class="btn btn-primary" (click)="crearOrden()">Generar</button>
                </div>
            </form>
        </div>

    </div>
</div>
FrankrP
  • 33
  • 7
  • why are you two-way binding value when you are using ngModel ? – MikNiller Nov 26 '19 at 15:38
  • Furthermore using () on value is not valid, there are plenty of similar questions on SO - https://stackoverflow.com/questions/42699705/ngmodel-vs-value – MikNiller Nov 26 '19 at 15:44

1 Answers1

0

You need to have the input like this

<input type="text" class="form-control" id="exampleFormControlInput1"
 placeholder="Nombres del Clientes" name="nombres_cliente" [(ngModel)]="clienteMkw.datos[0].nombre">

You can't have the value and ngModel at time

C.OG
  • 6,236
  • 3
  • 20
  • 38
Ashot Aleqsanyan
  • 4,252
  • 1
  • 15
  • 27
  • I did it and still get the problem... :/ – FrankrP Nov 26 '19 at 16:37
  • let me clarify. if you want to send to the backend `{ nombres_cliente: ""​ }` you need to have the ng model like this `[(ngModel)]="ordenServicio.nombres_cliente"`. if you want to send to the backend `clienteMkw.datos[0].nombre` you need to have the ng model like this `[(ngModel)]="clienteMkw.datos[0].nombre"` Please make sure you need to remove [value] from input – Ashot Aleqsanyan Nov 26 '19 at 16:44
  • Actually i want to send `[(ngModel)]="clienteMkw.datos[0].nombre"`, i have a button with a (click) event that request the data from an API a put in the input, and when i do get request it work, but if i try to send it to the backend it send empty values – FrankrP Nov 26 '19 at 17:30
  • can you put your html and ts files in your question? – Ashot Aleqsanyan Nov 26 '19 at 17:35
  • let me create stackblitz example. – Ashot Aleqsanyan Nov 26 '19 at 18:03
  • Look you need to bind the properties which you need to send to the backend. If you want to send to the backend `ordenServicio` you need to bind into html ngmodels the properties of `ordenServicio` I tried to reporoduce soem exaple based on your code please check this one https://stackblitz.com/edit/angular-template-driven-example-321 – Ashot Aleqsanyan Nov 26 '19 at 18:48
  • Yeah i understand that, but actually the problem is that the "values" of my inputs are fill dinamically by a `get` request that i get from `clienteMkw` object, and it's normal working cuz the inputs are fill, but when i send the values to the backend, it send empty values... – FrankrP Nov 26 '19 at 19:28
  • Please chekc the example https://stackblitz.com/edit/angular-template-driven-example-321 click on the button Obtener Mkw – Ashot Aleqsanyan Nov 26 '19 at 19:34
  • Look you need to get the data from API and set it into `ordenServicio`. Is this working for you? – Ashot Aleqsanyan Nov 26 '19 at 19:35
  • I forgot you have on your html `[ngValue]` . replace it to `[value]` – Ashot Aleqsanyan Nov 26 '19 at 19:43
  • 1
    I can see that you are using the same object yo send the data but i actually use two different object, i will try to set the data from the API into `ordenServicio` as you said – FrankrP Nov 26 '19 at 20:02
  • yes and I will recommend to you explore the reactive forms angular https://angular.io/guide/reactive-forms. I think it is much comfortable and easy to use . https://alligator.io/angular/reactive-forms-introduction/ – Ashot Aleqsanyan Nov 26 '19 at 21:16
  • Thanks for ur help Ashot, i'll check that, finally the solution was set the data in `ordenServicio` object! Thx again ^^ – FrankrP Nov 28 '19 at 03:03