DEV Community

seshubabubatchu
seshubabubatchu

Posted on

Angular set and patch value in Template driven forms

Hey Guys!!!
I was going through angular forms and found a topic quite confusing have gone through a lot of youtube tutorials which are different from another, every one explains in their own way
So decided to make a post on this in-depth.
we are going to see in how many ways set and patch value can be used in angular *template-driven forms *

and common errors that I have encountered while setting and patching values in angular forms

Straight into code
Image description
This is the template-driven form we are going to use in this blog
Image description
Image description
Image description
So one common error I was getting while setting and patching values without using any buttons and onclick events I mean when setting or patching values at the time on initialization in ngOnInit()
is
Image description
This error is quite annoying since there is no relevant info on the internet (maybe i did not search that great)
So to avoid this it is quite simple to use a setTimeOut function while setting or patching values and call that function on ngOnInit() this works perfectly fine
Image description

Ways to set and patch values :

There are many ways to set and patch values you can use them according to your convinience, all the methods can be found in the above screenshot which were commented

Thanks

Top comments (1)

Collapse
 
fabianlema profile image
Fabián Lema Pérez

But the applyForm isn't defined in OnInit cycle, you must use AfterViewInit cycle.