Hone logo
Hone
Problems

Angular FormsModule Integration Challenge

This challenge focuses on the practical application of Angular's FormsModule for handling user input and form submissions. You will create a simple Angular component that utilizes ngModel for two-way data binding to input fields and then demonstrates how to access and process the form's data upon submission. This is a fundamental skill for building interactive web applications in Angular.

Problem Description

Your task is to build an Angular component that allows users to input their name and email address. You will use FormsModule and the ngModel directive to achieve two-way data binding for these input fields. Additionally, you need to implement a button that, when clicked, triggers a method to display the collected name and email in the console.

Key Requirements:

  1. Component Creation: Create a new Angular component (e.g., UserProfileComponent).
  2. HTML Template: The component's template should contain:
    • An input field for the user's name.
    • An input field for the user's email.
    • A submit button.
  3. FormsModule Integration: Import and configure FormsModule in your Angular module (likely AppModule for this standalone component setup).
  4. Two-Way Data Binding: Use the [(ngModel)] directive to bind the name input to a name property in your component's class, and the email input to an email property.
  5. Form Submission Handling: Implement a method in your component class (e.g., onSubmit()) that is called when the submit button is clicked.
  6. Console Output: The onSubmit() method should log the current values of the name and email properties to the browser's developer console.

Expected Behavior:

  • As the user types into the "Name" input field, the name property in the component should update in real-time.
  • As the user types into the "Email" input field, the email property in the component should update in real-time.
  • When the "Submit" button is clicked, the current values of name and email should be printed to the console.

Edge Cases to Consider:

  • Empty Input: The application should handle cases where the user submits the form with empty name or email fields. The console output should reflect these empty values.

Examples

Example 1:

Component Class (TypeScript):

import { Component } from '@angular/core';

@Component({
  selector: 'app-user-profile',
  templateUrl: './user-profile.component.html',
  // No need for standalone: true if using AppModule
})
export class UserProfileComponent {
  name: string = '';
  email: string = '';

  onSubmit(): void {
    console.log('Name:', this.name);
    console.log('Email:', this.email);
  }
}

Component Template (HTML):

<div>
  <h2>User Profile</h2>
  <input type="text" [(ngModel)]="name" placeholder="Enter your name">
  <br><br>
  <input type="email" [(ngModel)]="email" placeholder="Enter your email">
  <br><br>
  <button (click)="onSubmit()">Submit</button>
</div>

Module Configuration (AppModule - if not using standalone components):

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms'; // Import FormsModule here

import { AppComponent } from './app.component';
import { UserProfileComponent } from './user-profile/user-profile.component'; // Assuming UserProfileComponent is in this path

@NgModule({
  declarations: [
    AppComponent,
    UserProfileComponent
  ],
  imports: [
    BrowserModule,
    FormsModule // Add FormsModule to imports
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Input:

  • User types "Alice" into the Name field.
  • User types "alice@example.com" into the Email field.
  • User clicks the "Submit" button.

Output (Console):

Name: Alice
Email: alice@example.com

Explanation: The ngModel directive successfully bound the input values to the name and email properties. The onSubmit method then accessed these updated properties and logged them to the console.

Example 2:

Input:

  • User leaves the Name field blank.
  • User types "test@domain.com" into the Email field.
  • User clicks the "Submit" button.

Output (Console):

Name:
Email: test@domain.com

Explanation: The challenge correctly handles the empty name field, demonstrating that ngModel reflects the current state of the input, even if it's empty.

Constraints

  • Your Angular application should be set up using the Angular CLI.
  • The solution must use TypeScript.
  • The FormsModule must be explicitly imported and included in the imports array of the relevant Angular module.
  • No external libraries other than Angular's core modules are permitted.

Notes

  • Remember to declare or import your UserProfileComponent into the appropriate Angular module. If you are using standalone components, ensure you import FormsModule directly into your component's imports array.
  • The FormsModule is essential for enabling ngModel. Without it, the [(ngModel)] directive will not work, and you'll likely encounter build errors.
  • Think about how the [(ngModel)] syntax achieves both data binding from the component to the input (model to view) and from the input to the component (view to model) simultaneously.
Loading editor...
typescript