Consider this Angular standalone component with a simple form. What will be the console output when the user submits the form with the input value 'hello'?
import { Component } from '@angular/core'; import { FormsModule } from '@angular/forms'; @Component({ selector: 'app-simple-form', standalone: true, template: ` <form (ngSubmit)="onSubmit()"> <input type="text" [(ngModel)]="name" name="name" required /> <button type="submit">Submit</button> </form> `, imports: [FormsModule] }) export class SimpleFormComponent { name = ''; onSubmit() { console.log(`Submitted name: ${this.name}`); } }
Think about how two-way binding with [(ngModel)] works and when onSubmit is called.
The [(ngModel)] binds the input value to the name property. When the form is submitted, onSubmit logs the current name value, which is 'hello' if typed by the user.
Given a component property email, which template snippet correctly binds an input field to it for form submission?
Remember the Angular syntax for two-way binding uses [(ngModel)] and requires a name attribute inside forms.
Option A uses the correct two-way binding syntax with [(ngModel)] and includes the required name attribute. Option A is a manual binding but misses form integration. Options C and D are invalid syntax.
submittedData after submitting this reactive form?Look at this Angular component using reactive forms. After the user types 'Angular' in the input and submits, what is the value of submittedData?
import { Component } from '@angular/core'; import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; @Component({ selector: 'app-reactive-form', standalone: true, imports: [ReactiveFormsModule], template: ` <form [formGroup]="form" (ngSubmit)="onSubmit()"> <input formControlName="framework" /> <button type="submit">Submit</button> </form> ` }) export class ReactiveFormComponent { form = new FormGroup({ framework: new FormControl('') }); submittedData: string | null = null; onSubmit() { this.submittedData = this.form.value.framework; } }
Reactive forms store input values in the form controls. The onSubmit method assigns the control's value to submittedData.
The user input 'Angular' is stored in the 'framework' control. On submit, submittedData is set to this value, so it becomes 'Angular'.
Given this component, the console logs 'Submitted value: ' with an empty string even after typing text. What is the cause?
import { Component } from '@angular/core'; import { FormsModule } from '@angular/forms'; @Component({ selector: 'app-buggy-form', standalone: true, imports: [FormsModule], template: ` <form (ngSubmit)="submit()"> <input type="text" [(ngModel)]="inputValue" name="inputValue" /> <button type="submit">Send</button> </form> ` }) export class BuggyFormComponent { inputValue = ''; submit() { console.log(`Submitted value: ${this.inputValue}`); } }
Check if the necessary Angular modules are imported to enable ngModel two-way binding.
Without importing FormsModule, Angular does not process ngModel bindings properly, so the property inputValue never updates from the input field.
You want to submit a form and reset it only if the server confirms success asynchronously. Which approach correctly handles this?
Think about how to handle asynchronous operations and only reset after success.
Option B waits for the server response before resetting the form, ensuring the form is only cleared on success. Other options reset too early or arbitrarily.