3

What is the difference between when i get the values in Reactive forms in below ways:

this.someForm.controls['firstName'].value
this.someForm.get('firstName').value

public someForm: FormGroup = this.formBuilder.group({
  firstName: ['', Validators.required],
});

this.someForm.controls['firstName'].value
this.someForm.get('firstName').value<br>

Above is my form and two different ways to get values from the form. But what is the difference b/w both ways if any ?

insertusernamehere
  • 21,742
  • 7
  • 80
  • 113
  • 2
    https://netbasal.com/angular-reactive-forms-tips-and-tricks-bb0c85400b58, basically is not diference (at the moment), but nobody sure us that this not change in a future – Eliseo Aug 08 '19 at 11:48
  • Ya i also use to think that there are no differences but i saw some diff behaviour when i used valueChanges in both scenarios. – Ankit singh Aug 08 '19 at 12:11

1 Answers1

0

There are not many differences between these two but Angular team might opt to change the FormGroup structure in the future, and if we use the controls property directly, it might lead to a breaking change, whereas the get() method could adjust to reflect the new structure.

FormGroup.get is designed to access target formcontrol by it's path, which makes it easy to get the target control from multi layer embed form and also makes code clear and easily to understand.

There is not much info about this but you can read something very much similar here,

AlexElin
  • 123
  • 2
  • 8
ganesh045
  • 4,099
  • 2
  • 11
  • 32