What is the difference between using a normal direct property assignment or using a getter to set the value of a property in Angular?


For example, what is the difference between this:

get name() { return this.formGroup.get('name') }

and this:

public name = this.formGroup.get('name')



When you use a direct assignment you basically assign the value and that’s it. There is no reference to the original value. This means that when the formGroup value gets updated, the name value will keep the value it got when originally assigned (unless you reassign it).

When you use a getter, the value will always be whatever is returned inside the getter. That means that every time the method is called, it will return the actual value from the form control. So for example, if you want to display the form control value in the HTML, you should use the getter because change detection will cause the actual value to be displayed.

Answered By – Nathan

Answer Checked By – Terry (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.