liveBook · Manning

FormsModule

As explained in Modern Angular

FormsModule is an Angular module that provides directives and providers for template-driven and reactive forms.

FormsModule provides Angular directives and providers for template-driven and reactive forms. In a standalone component, add it to the component-level imports array to enable ngModel. section 2.3.1

Standalone login form

A LoginComponent imports FormsModule and binds email and password inputs with [(ngModel)]="credentials.email" and [(ngModel)]="credentials.password". This keeps entered credentials synchronized with component state. section 2.3.1

Listing 2.7 Standalone component importing a module
@Component({
    selector: 'app-login',
    template: `
        <div class="login-container">
            <h1>Login</h1>
            <form>
                <input type="text" name="email"
                    placeholder="Email"
                    [(ngModel)]="credentials.email" />    #1
                <input type="password" name="password" placeholder="Password" 
                   [(ngModel)]="credentials.password">
                <button type="submit">Login</button>
            </form>
        </div>
    `,
    standalone: true,
    imports: [FormsModule],     #2
})
export class LoginComponent {
    credentials = { email: '', password: '' };
}

Shows the component-level FormsModule import alongside two-way ngModel bindings. section 2.3.1

How Modern Angular covers it

How deep it goes
Demonstrates template-driven binding and stops short of detailed reactive-form implementation.
Examples
Uses a standalone login form with email and password fields bound to a credentials object.
What it assumes
Assumes familiarity with Angular components and templates while demonstrating how standalone components import NgModules.
What you can do after
Apply it via Angular component imports to build a bound standalone login form.
How it connects
Connects FormsModule with standalone components, NgModule interoperability, ngModel, and form validation messaging.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern Angular. The section links point to the parts of the book it draws on.