148

I am aware that I can get the values of a form using

JSON.stringify(this.formName.value)

However, I want to get a single value from the form.

How do I go about doing that?

Snackoverflow
  • 5,332
  • 7
  • 39
  • 69
Stephen Romero
  • 2,812
  • 4
  • 25
  • 48

8 Answers8

229

You can get value like this

this.form.controls['your form control name'].value
RemyaJ
  • 5,358
  • 4
  • 22
  • 41
188

Yes, you can.

this.formGroup.get('name of you control').value
Julia Passynkova
  • 17,256
  • 6
  • 33
  • 32
  • 17
    This should be right answer since you access via method rather than getting controls directly – Mohan Ram Aug 23 '18 at 12:58
  • 2
    Of course, this is the method used in the official documentation too. https://angular.io/guide/reactive-forms – Kavinda Jayakody May 03 '20 at 13:35
  • I want this to work - but depending on where you're using it in a component life cycle it may throw an error regarding `get()` not being available on the form. Where as using the `this.form.value.{name}` will properly return the value. – Levidps Mar 20 '23 at 04:17
38

Dot notation will break the type checking, switch to bracket notation. You might also try using the get() method. It also keeps AOT compilation in tact I've read.

this.form.get('controlName').value // safer
this.form.controlName.value // triggers type checking and breaks AOT
Ben Racicot
  • 5,332
  • 12
  • 66
  • 130
19

for Angular 6+ and >=RC.6

.html

<form [formGroup]="formGroup">
  <input type="text" formControlName="myName">
</form>

.ts

public formGroup: FormGroup;
this.formGroup.value.myName

should also work.

billyjov
  • 2,778
  • 19
  • 35
12

Another option:

this.form.value['nameOfControl']
hurlman
  • 366
  • 3
  • 6
4

You can use getRawValue()

this.formGroup.getRawValue().attribute
osmanraifgunes
  • 1,438
  • 2
  • 17
  • 43
3

This code also works:

this.formGroup.controls.nameOfcontrol.value
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
1

You can do by the following ways

this.your_form.getRawValue()['formcontrolname]
this.your_form.value['formcontrolname]
Ahmad Sharif
  • 4,141
  • 5
  • 37
  • 49