SoFunction
Updated on 2025-04-13

Detailed explanation of Angular 4 forms quick start

Basic knowledge

Basic use of Angular CLI

Install the Angular CLI (optional)

npm install -g @angular/cli

Create a new project

ng new PROJECT-NAME

Start the local server

cd PROJECT-NAME
ng serve

Angular Forms Introduction

There are two forms in Angular 4:

  1. Template Driven Forms - Template-driven form (similar to forms in AngularJS)
  2. Reactive Forms - Responsive Forms

This article mainly introduces the basic knowledge of Template Driven Forms, and the relevant knowledge points will be introduced in the form of questions and answers.

Section 1 - Create the easiest input box

How to achieve two-way binding?

In Angular form, we implement two-way binding through the ngModel directive.

import { Component } from '@angular/core';

@Component({
 selector: 'app-root',
 template: `
  <input type="text" [(ngModel)]="username">
  {{username}}
 `,
})
export class AppComponent {
 username = 'semlinker';
}

Section 2 - Adding simple verification function

How to add verification function to form controls?

Currently, the built-in validators supported by Angular are as follows:

  1. required - Set the form control value to be non-empty
  2. email - Set the format of the form control value as email
  3. minlength - Set the minimum length of the form control value
  4. maxlength - Set the maximum length of the form control value
  5. pattern - Set the value of the form control to match the pattern corresponding to the pattern

Next, let’s add the simplest required verification.

import { Component } from '@angular/core';

@Component({
 selector: 'app-root',
 template: `
  <input 
   type="text" 
   required
   [(ngModel)]="username">
  {{username}}
 `,
})
export class AppComponent {
 username = 'semlinker';
}

How to determine whether the form control has passed the verification?

In Angular we can pass #userName="ngModel"Method to obtainngModel object, then pass Determine whether the form control passes verification.

import { Component } from '@angular/core';

@Component({
 selector: 'app-root',
 template: `
  <input 
   type="text" 
   required
   [(ngModel)]="username"
   #userName="ngModel">
  {{}}
 `,
})
export class AppComponent {
 username = 'semlinker';
}

Section 3 - Display Error Message of Failed Verification

How to display error message for verification failure?

In Angular we can pass #userName="ngModel" Method to obtainngModel object, then pass through the object'serrors attribute to obtain the verification status of the corresponding verification rules (such as required, minlength, etc.).

import { Component } from '@angular/core';

@Component({
 selector: 'app-root',
 template: `
  &lt;input 
   type="text" 
   required
   minlength="3"
   [(ngModel)]="username"
   #userName="ngModel"&gt;
  &lt;hr&gt;
  &lt;div *ngIf="?.required"&gt;Please enter your username&lt;/div&gt;
  &lt;div *ngIf="?.minlength"&gt;
   The length of the username must be greater than {{?.}},The current length is
    {{?.}}
  &lt;/div&gt;
 `,
})
export class AppComponent {
 username = 'semlinker';
}

Section 4 - Creating a Form

How to use a form?

In Angular, we can use the familiar <form> tag to create forms.

import { Component } from '@angular/core';

@Component({
 selector: 'app-root',
 template: `
 &lt;form&gt;
  &lt;input 
   type="text" 
   required
   minlength="3"
   name="username"
   [(ngModel)]="username"
   #userName="ngModel"&gt;
  &lt;hr&gt;
  &lt;div *ngIf="?.required"&gt;Please enter your username&lt;/div&gt;
  &lt;div *ngIf="?.minlength"&gt;
   The length of the username must be greater than {{?.}},The current length is
    {{?.}}
  &lt;/div&gt;
  &lt;button type="submit"&gt;submit&lt;/button&gt;
 &lt;/form&gt;
 `,
})
export class AppComponent {
 username = 'semlinker';
}

It should be noted that when using<form> After the tag, ourusername Input box, must be addedname property.

How to get the value of a form submitted?

In Angular we can pass #loginForm="ngForm" Method to obtainngForm object, then pass to get the value of the form.

import { Component } from '@angular/core';

@Component({
 selector: 'app-root',
 template: `
 &lt;form #loginForm="ngForm" (ngSubmit)="onSubmit()"&gt;
  &lt;input 
   type="text" 
   required
   minlength="3"
   name="username"
   [(ngModel)]="username"
   #userName="ngModel"&gt;
  &lt;hr&gt;
  &lt;div *ngIf="?.required"&gt;Please enter your username&lt;/div&gt;
  &lt;div *ngIf="?.minlength"&gt;
   The length of the username must be greater than {{?.}},The current length is
    {{?.}}
  &lt;/div&gt;
  &lt;button type="submit"&gt;submit&lt;/button&gt;
  {{ | json}}
 &lt;/form&gt;
 `,
})
export class AppComponent {
 username = 'semlinker';

 onSubmit(value) {
  (value);
 }
}

Section 5 - Introduction to ngModelGroup

What is the function of ngModelGroup?

ngModelGroup Directives are another special directive provided in Angular forms. They can group the input content of the form, making it easier for us to semantically distinguish inputs of different properties. For example, the contact person's information includes name and address. Now the name and address need to be collected in detail. The name can be refined into surnames and names, and the address can be refined into cities, districts, streets, etc.

import { Component } from '@angular/core';

@Component({
 selector: 'app-root',
 template: `
 &lt;form #loginForm="ngForm" (ngSubmit)="onSubmit()"&gt;
  &lt;fieldset ngModelGroup="user"&gt;
  &lt;input 
   type="text" 
   required
   minlength="3"
   name="username"
   [(ngModel)]="username"
   #userName="ngModel"&gt;
  &lt;hr&gt;
  &lt;div *ngIf="?.required"&gt;Please enter your username&lt;/div&gt;
  &lt;div *ngIf="?.minlength"&gt;
   The length of the username must be greater than {{?.}},The current length is
    {{?.}}
  &lt;/div&gt;
  &lt;input type="password" ngModel name="password"&gt;
  &lt;/fieldset&gt;
  &lt;button type="submit"&gt;submit&lt;/button&gt;
  &lt;hr&gt;
  {{ | json}}
 &lt;/form&gt;
 `,
})
export class AppComponent {
 username = 'semlinker';

 onSubmit(value) {
  (value);
 }
}

After the above code is successfully run,{{ | json}} Output result:

{ "user": { "username": "semlinker", "password": "123" } }

Section 6 - Add verification status style for form

How to add validation status style information to a form?

In Angular form, if the verification is passed, it will be added to the form control ng-valid Class, if verification fails, it will be added to the form controlng-invalid kind.

import { Component } from '@angular/core';

@Component({
 selector: 'app-root',
 styles: [`
  -invalid {
    border: 3px solid red;
  }
  -valid {
    border: 3px solid green;
  }
 `
 ],
 template: `
 &lt;form #loginForm="ngForm" (ngSubmit)="onSubmit()"&gt;
  &lt;fieldset ngModelGroup="user"&gt;
  &lt;input 
   type="text" 
   required
   minlength="3"
   name="username"
   [(ngModel)]="username"
   #userName="ngModel"&gt;
  &lt;hr&gt;
  &lt;div *ngIf="?.required"&gt;Please enter your username&lt;/div&gt;
  &lt;div *ngIf="?.minlength"&gt;
   The length of the username must be greater than {{?.}},The current length is
    {{?.}}
  &lt;/div&gt;
  &lt;input type="password" required ngModel name="password"&gt;
  &lt;/fieldset&gt;
  &lt;button type="submit"&gt;submit&lt;/button&gt;
  &lt;hr&gt;
  {{ | json}}
 &lt;/form&gt;
 `,
})
export class AppComponent {
 username = 'semlinker';

 onSubmit(value) {
  (value);
 }
}

Section 7 - State of form control

In addition to form controlsvalid What other states are included in addition to states?

In Angular, form control has the following 6 states, which we can use#userName="ngModel" Method to obtainngModel object, and then obtain the status information of the control. The specific status is as follows:

  1. valid - Form control is valid
  2. invalid - form control is invalid
  3. pristine - Form control value has not changed
  4. dirty - Form control value has changed
  5. touched - Form control has been accessed
  6. untouched - Form control not visited
import { Component } from '@angular/core';

@Component({
 selector: 'app-root',
 styles: [`
  -invalid {
    border: 3px solid red;
  }
  -valid {
    border: 3px solid green;
  }
 `
 ],
 template: `
 &lt;form #loginForm="ngForm" (ngSubmit)="onSubmit()"&gt;
  &lt;fieldset ngModelGroup="user"&gt;
  &lt;input 
   type="text" 
   required
   minlength="3"
   name="username"
   [(ngModel)]="username"
   #userName="ngModel"&gt;
  &lt;hr&gt;
  &lt;p&gt;NameControl'svalidstate:{{}} - Indicates that the control is valid&lt;/p&gt;
  &lt;p&gt;NameControl'sinvalidstate:{{}} - Indicates that the control is invalid&lt;/p&gt;
  &lt;p&gt;NameControl'spristinestate:{{}} - Indicates that the control value has not changed&lt;/p&gt;
  &lt;p&gt;NameControl'sdirtystate:{{}} - Indicates that the control value has changed&lt;/p&gt;
  &lt;p&gt;NameControl'stouchedstate:{{}} - Indicates that the control has been accessed&lt;/p&gt;
  &lt;p&gt;NameControl'suntouchedstate:{{}} - Indicates that the control has not been accessed&lt;/p&gt;
  &lt;div *ngIf="?.required"&gt;Please enter your username&lt;/div&gt;
  &lt;div *ngIf="?.minlength"&gt;
   The length of the username must be greater than {{?.}},The current length is
    {{?.}}
  &lt;/div&gt;
  &lt;input type="password" required ngModel name="password"&gt;
  &lt;/fieldset&gt;
  &lt;button type="submit"&gt;submit&lt;/button&gt;
  &lt;hr&gt;
  {{ | json}}
 &lt;/form&gt;
 `,
})
export class AppComponent {
 username = 'semlinker';

 onSubmit(value) {
  (value);
 }
}

Section 8 - Using a radio control

How to add a radio control?

In Angular, we pass<input name="***" type="radio"> Add a radio control.

import { Component } from '@angular/core';

@Component({
 selector: 'app-root',
 template: `
 &lt;form #loginForm="ngForm"&gt;
  AngularVersion:
  &lt;div *ngFor="let version of versions;"&gt;
    &lt;input 
     []="version"
      name="version"
      ngModel
      required
      [value]="version"
      type="radio"&gt;
     &lt;label []="version"&gt;{{version}}&lt;/label&gt;
   &lt;/div&gt;
  &lt;hr&gt;
  {{ | json}}
 &lt;/form&gt;
 `,
})
export class AppComponent {
 versions = ['', '', ''];
}

Section 9 - Using Multiple Choice Controls

How to add a multi-select control?

In Angular, we pass<select name="***"> Add multiple choice controls.

import { Component } from '@angular/core';

@Component({
 selector: 'app-root',
 template: `
 &lt;form #loginForm="ngForm"&gt;
  AngularVersion:
  &lt;select name="version" [ngModel]="versions[0]"&gt;
     &lt;option
      *ngFor="let version of versions;"
      [value]="version"&gt;
       {{version}}
    &lt;/option&gt;
   &lt;/select&gt;
  &lt;hr&gt;
  {{ | json}}
 &lt;/form&gt;
 `,
})
export class AppComponent {
 versions = ['', '', ''];
}

How to add required verification?

import { Component } from '@angular/core';

@Component({
 selector: 'app-root',
 styles: [`
  -invalid + label:after {
   content: '<-- Please select a version!'
  }
 `
 ],
 template: `
 &lt;form #loginForm="ngForm"&gt;
  AngularVersion:
  &lt;div&gt;
   &lt;select name="version" [ngModel]="version" required&gt;
    &lt;option
    *ngFor="let version of versions;"
     [value]="version"&gt;
     {{version}}
    &lt;/option&gt;
   &lt;/select&gt;
   &lt;label&gt;&lt;/label&gt;
  &lt;/div&gt;
  &lt;hr&gt;
  {{ | json}}
 &lt;/form&gt;
 `,
})
export class AppComponent {
 versions = ['','', '', ''];
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.