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.
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.
ng buildThe 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.
[(ngModel)]gives two-way binding for simple forms.- Use reactive forms for big forms with validation.
ng buildcreates production files indist/.
