Initialize using patchValue for template driven forms

144
January 10, 2020, at 11:10 AM

I use template driven and am trying to initialize my form using patchValue and it is not working.

I can make it work if I use two-way binding to set values [()] or even setTimeout but I was just wondering, is a way to make it work with just patchValue?

https://stackblitz.com/edit/angular-fft2c5

Thank you.

Answer 1

In template driven forms, you need to bind [(ngModel] to set values.

Try like this:

.html

<input name="name" [(ngModel)]="formValue.name"/>

.ts

formValue:any = {}
ngOnInit() {
  this.formValue = {name: this.name}
}

Working Demo

READ ALSO
Re-posting &lt;input&gt; value into &lt;textarea&gt;

Re-posting <input> value into <textarea>

I want to re-generate user's input values onto textarea with specific formatI've created input and 'select' with various 'options' inside as well as 'button' that triggers the function

86
currently no loaders are configured to process this file form vuepress

currently no loaders are configured to process this file form vuepress

I want to import third-party libraries went I use the vuepressI should config a rule to compile node_modules js file

328