Angular forms FormControlName Directive - GeeksforGeeks (2024)

Improve

Improve

Like Article

Like

Save

Report

In this article, we are going to see what is FormControlName in Angular 10 and how to use it.

FormControlName is used to sync a FormControl in an existing FormGroup to a form control element by name.

Syntax:

<form [FormControlName] ="name">

Exported from:

  • ReactiveFormsModule

Selectors:

  • [FormControlName]

Approach:

  • Create the Angular app to be used
  • In app.component.ts make an object that contain value for the input.
  • In app.component.html use FormControlNameto get values.
  • Serve the angular app using ng serve to see the output.

Example 1:

app.component.ts

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

import { FormGroup, FormControl, FormArray } from '@angular/forms'

@Component({

selector: 'app-root',

templateUrl: './app.component.html',

styleUrls: [ './app.component.css' ]

})

export class AppComponent {

form = new FormGroup({

name: new FormControl(),

rollno: new FormControl()

});

get name(): any {

return this.form.get('name');

}

get rollno(): any {

return this.form.get('rollno');

}

onSubmit(): void {

console.log(this.form.value);

}

}

app.component.html

<br>

<form [formGroup]="form" (ngSubmit)="onSubmit()">

<input formControlName="name" placeholder="Name">

<input formControlName="rollno" placeholder="RollNo">

<br>

<button type='submit'>Submit</button>

<br>

<br>

</form>

Output:

Angular forms FormControlName Directive - GeeksforGeeks (1)

Reference: https://angular.io/api/forms/FormControlName



Last Updated : 03 Jun, 2021

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

Angular forms FormControlName Directive - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 6109

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.