← Learning Hub
Beginner

Forms and Building Your App

A simple form

For a simple form, add FormsModule to imports and use [(ngModel)]. It keeps a text box and a value in sync both ways.

Forms are a big part of most business apps: sign-up, checkout, search filters, admin screens. Angular offers two styles. Template-driven forms with ngModel are quick for small forms. Reactive forms define the form in the TypeScript class, which suits large forms with many validation rules.

Example
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-search',
  imports: [FormsModule],
  template: `
    <input [(ngModel)]="text" placeholder="Search a product" />
    <p>You are searching for: <b>{{ text }}</b></p>
  `,
})
export class Search {
  text = '';
}
[( )] is called "banana in a box". It is property binding and event binding together.

Build for your website

When your app is ready, build the final files:

ng build compiles TypeScript, removes unused code, shrinks the files and gives each file a unique name for caching. The result is plain HTML, CSS and JavaScript that any web server can host - no Angular or Node.js needed on the server just to serve it.

Example
ng build

The finished files are saved in the dist/ folder. Upload them to any web host.

What to learn next

  • HttpClient to load data from a server.
  • Reactive forms for bigger forms with checks.
  • Testing your components with ng test.
Key points
  • [(ngModel)] gives two-way binding for simple forms.
  • Use reactive forms for big forms with validation.
  • ng build creates production files in dist/.

📝 Test yourself

1. Which syntax keeps an input and a value in sync both ways?
2. Where does ng build save the finished files?
3. Fill in the module needed for ngModel:
imports: []