Performance: FormsModule setup
This affects initial page load speed and interaction responsiveness by adding form handling capabilities and directives.
import { Component } from '@angular/core'; import { FormsModule } from '@angular/forms'; @Component({ selector: 'app-simple-form', standalone: true, imports: [FormsModule], template: `<input [(ngModel)]="name">` }) export class SimpleFormComponent { name = ''; }
import { Component } from '@angular/core'; @Component({ selector: 'app-simple-form', template: `<input [(ngModel)]="name">` }) export class SimpleFormComponent { name = ''; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Without FormsModule | No ngModel directives, no DOM updates for binding | 0 | Minimal | [X] Bad |
| With FormsModule imported | ngModel directives add event listeners and DOM updates | 1 per input change | Low | [OK] Good |
FormsModule in an Angular module?FormsModule provides directives like ngModel that enable template-driven forms and two-way data binding.FormsModule.FormsModule in a standalone Angular component?imports array to use their features.FormsModule must be in imports, not in declarations or providers.<input [(ngModel)]="username" />
@Component({
standalone: true,
imports: [FormsModule]
})
export class MyComponent {
username = 'Alice';
}FormsModule and uses [(ngModel)] binding on username.username property is initialized to 'Alice', so the input box will show 'Alice' initially.[(ngModel)]? @Component({
standalone: true,
imports: []
})
export class MyComponent {
email = '';
}<input [(ngModel)]="email" />
[(ngModel)] but FormsModule is not imported, so Angular won't recognize ngModel.FormsModule to use ngModel; declarations are not needed for modules.userName. Which setup correctly enables two-way binding and accessibility?@Component({
standalone: true,
imports: [/* ? */]
})
export class MyComponent {
userName = '';
}<label for="nameInput">Name:</label> <input id="nameInput" type="text" [(ngModel)]="userName" aria-label="User name input" />
[(ngModel)] requires FormsModule to work in template-driven forms.for linked to input id, and input has aria-label, ensuring accessibility.Pass the test and this pattern moves unknown