0

Tengo un formulario, donde en el input tengo asignado el [(ngModel)]= "authorValue", pero recibo "undefined" como valor.

  authorValue: string;
  
  createAuthor(event: Event){
    event.preventDefault();
    // aca recibo undefined
    console.log('input value: ' + this.authorValue);
    this.author.name = this.authorValue;
    this.authorService.createAuthor(this.author);
    this.authorService.createAuthor(this.author).subscribe(
     res => {
     console.log('autor guardado: ' + res);
     },
     err => console.error(err)
     );

  }

aca el formulario

<form (ngSubmit)="createAuthor($event)">
  <div class="form-group">
  <label>Nombre de autor</label>
  <input type="text" class="form-control" [(ngModel)]="authorValue" placeholder="Ingrese nombre de autor">
 <button type="submit" class="btn btn-block">Guardar</button>
  </div>
</form>

1 respuesta 1

1

Cuando se usa ngModel en un formulario el input debe tener el atributo name. Si revisas la consola te debe estar mostrando ese error.

<form (ngSubmit)="createAuthor($event)">
      <div class="form-group">
      <label>Nombre de autor</label>
      <input type="text" class="form-control" [(ngModel)]="authorValue" 
      name="author" 
      placeholder="Ingrese nombre de autor">
     <button type="submit" class="btn btn-block">Guardar</button>
      </div>
    </form>

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.