How do I set the default value in a form field in Angular 2+?

Hello, I make fields for editing an object in angular, I ran into a problem when the fields already passed to value are not processed when the form is submitted, or rather they are processed only when you make edits to them, here is an example of how to deal with this?

<form [formGroup]="myForm" (ngSubmit)="onSubmit(myForm)">
  <input formControlName="title" type="text" class="form-control" placeholder="Название" value="{{ content?.title }}"/> 
   <input formControlName="body" type="text" class="form-control" placeholder="Контент" value="{{ content?.body }}"/> 
  <input type="submit" value="Редактировать"/> 
</form>
Author: Виктор, 2017-08-15

1 answers

When working with Data-Driven forms, to change the form values, you need to access the form instance created in the component class and change the values via setValue(). In this example, it will look like:

this.myForm.get('title').setValue = yourAsyncValue;
 2
Author: Artsiom, 2017-08-15 10:13:08