Angular Form Controls (Input, Select) Tutorial

beginner
5 min

Angular Form Controls (Input, Select) Tutorial

Welcome back to CodeYourCraft! Today, we're going to dive into a fascinating world of Angular form controls - specifically, input and select elements. Let's get started! šŸŽÆ

Understanding Form Controls

Form controls in Angular are used to manage user interactions with forms. They provide a simple and effective way to collect user input. In this lesson, we will focus on the two most common form controls: Input and Select. šŸ“

Input Form Control

The Input form control is used to collect single-line text data from users. Here's a simple example:

typescript
import { Component } from '@angular/core'; @Component({ selector: 'app-input-example', template: ` <input [(ngModel)]="name" type="text" placeholder="Enter your name"> <p>Your name is: {{ name }}</p> ` }) export class InputExampleComponent { name: string = ''; }

In this example, we have an Input field where users can enter their names. The ngModel directive binds the name variable to the input field. āœ…

šŸ’” Pro Tip: The ngModel directive allows two-way data binding, meaning it synchronizes the value of the form control with the corresponding property in the component.

Form Control Validation

Form validation is crucial to ensure user-entered data is correct. Angular provides built-in validators to help with this. Here's an example:

typescript
import { Component, NgModule } from '@angular/core'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; @Component({ selector: 'app-input-validation', template: ` <form #form="ngForm"> <input name="name" ngModel required minlength="3" /> <button type="submit" [disabled]="!form.valid">Submit</button> </form> ` }) export class InputValidationComponent { } @NgModule({ imports: [FormsModule, ReactiveFormsModule], declarations: [InputValidationComponent] }) export class InputValidationModule { }

In this example, we've added a required attribute to ensure the user enters something in the input field. We've also added a minlength attribute to ensure the entered name is at least 3 characters long. The submit button is disabled until the form is valid. āœ…

Select Form Control

The Select form control is used to create dropdown menus. Here's a simple example:

typescript
import { Component } from '@angular/core'; @Component({ selector: 'app-select-example', template: ` <select [(ngModel)]="selectedOption"> <option *ngFor="let option of options" [value]="option">{{ option }}</option> </select> <p>Selected option: {{ selectedOption }}</p> ` }) export class SelectExampleComponent { selectedOption: string = ''; options: string[] = ['Option 1', 'Option 2', 'Option 3']; }

In this example, we have a Select field populated with options. The ngModel directive binds the selectedOption variable to the selected option in the dropdown. āœ…

Quiz Time!

Quick Quiz
Question 1 of 1

What is the purpose of the `ngModel` directive in Angular?

That's it for today! In the next lesson, we'll explore more advanced features of Angular form controls, such as form groups and custom validators. Stay tuned! šŸš€

Happy coding! šŸ’»šŸ’¬