The most straightforward approach to building forms in Angular is to take advantage of the directives provided for you.
Copy <form method="POST" action="/register" id="signup-form">
<label for="email">Email</label>
<input type="text" name="email" id="email">
<label for="password">Password</label>
<input type="password" name="password" id="password">
<button type="submit">Sign Up</button>
</form>
Copy <signup-form>Loading...</signup-form>
Copy <form #signupForm="ngForm" (ngSubmit)="registerUser(signupForm)">
<label for="email">Email</label>
<input type="text" name="email" id="email" ngModel>
<label for="password">Password</label>
<input type="password" name="password" id="password" ngModel>
<button type="submit">Sign Up</button>
</form>
Copy import { Component } from '@angular/core' ;
import { NgForm } from '@angular/forms' ;
@ Component ({
selector : 'app-signup-form' ,
templateUrl : 'app/signup-form.component.html' ,
})
export class SignupFormComponent {
registerUser (form : NgForm ) {
console .log ( form .value);
// {email: '...', password: '...'}
// ...
}
}