html - Angular 2 When I submit the form the data is coming empty -
i'm new angular 2, finished studied angular 1x , knew has increased version.
my problem i'm not getting data on component.
html :
<div class="container"> <h1 class="text-center"> cadastro teste </h1> <form (submit)="cadastro($event)" > <div class="row"> <div class="col-lg-12"> <div class="form-group"> <label> nome : </label> <input type="text" (ngmodel)="pessoa.nome" name="nome" class="form-control"/> </div> </div> </div> <div class="row"> <div class="col-lg-12"> <div class="form-group"> <label> email : </label> <input type="text" (ngmodel)="pessoa.email" name="email" class="form-control"/> </div> </div> </div> <div class="row"> <div class="col-lg-12"> <input type="submit" value="cadastrar" class="btn btn-primary" /> </div> </div> </form> </div>
component :
import {component} '@angular/core'; @component({ moduleid : module.id, selector : 'teste', templateurl : './teste.component.html' }) export class testecomponent{ pessoa; cadastro(event){ event.preventdefault(); console.log(this.pessoa); } }
i'm using typescript
try in template:
<form #form="ngform" (submit)="cadastro($event, form)" > </form>
in component change function this:
cadastro(event:event, form: ngform): void { console.log('form data', form.value); ... }
and add import ngform in top:
import { ngform } '@angular/forms';
Comments
Post a Comment