Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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')

Thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading