Example
Template - signup.component.html
<form #signUpForm="ngForm" (ngSubmit)="onSubmit()">
<div class="title">
Sign Up
</div>
<div class="input-field">
<label for="username">username</label>
<input
type="text"
pattern="\w{4,20}"
name="username"
required="required"
[(ngModel)]="signUpRequest.username" />
</div>
<div class="input-field">
<label for="email">email</label>
<input
type="email"
pattern="^\S+@\S+$"
name="email"
required="required"
[(ngModel)]="signUpRequest.email" />
</div>
<div class="input-field">
<label for="password">password</label>
<input
type="password"
pattern=".{6,30}"
required="required"
name="password"
[(ngModel)]="signUpRequest.password" />
</div>
<div class="status">
{{ status }}
</div>
<button [disabled]="!signUpForm.form.valid" type="submit">
<span>Sign Up</span>
</button>
</form>
Component - signup.component.ts
import { Component } from '@angular/core';
import { SignUpRequest } from './signup-request.model';
@Component({
selector: 'app-signup',
templateUrl: './signup.component.html',
styleUrls: ['./signup.component.css']
})
export class SignupComponent {
status: string;
signUpRequest: SignUpRequest;
constructor() {
this.signUpRequest = new SignUpRequest();
}
onSubmit(value, valid) {
this.status = `User ${this.signUpRequest.username} has successfully signed up`;
}
}
Model - signup-request.model.ts
export class SignUpRequest {
constructor(
public username: string="",
public email: string="",
public password: string=""
) {}
}
App Module - app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { SignupComponent } from './signup/signup.component';
@NgModule({
declarations: [
AppComponent,
SignupComponent
],
imports: [
BrowserModule,
FormsModule
],
bootstrap: [AppComponent]
})
export class AppModule { }
App Component - app.component.html
<app-signup></app-signup>