0

This is a simplified version of my code:

export class CustomerDetails implements OnInit {
  customer: FormGroup;

  constructor() { }

  ngOnInit() {
    initForm();
  }

  initForm(): void {
    this.customer = new FormGroup({
     name: new FormControl("John Doe"),
     address: new FormControl("123 Fake Street")
    });

  }
}

Here is my Template:

<form [formGroup]="customer">
  <div class="control" formGroupName="customer">
    <input type="text" name="name" formControlName="name">
  </div>
  <div class="control" formGroupName="customer">
    <input type="text" name="name" formControlName="address">
  </div>
</form>

I even tried wrapping everything between the form element in an

<ng-container formGroupName="customer"></ng-container>

But the values of the text inputs are blank. Am I missing something?

2
  • <input placeholder="{{ placeholder }}" [formControl]="inputControl" /> Commented Mar 12, 2022 at 1:38
  • You don't need the wrapper <div class="control" formGroupName="customer">. Commented Mar 12, 2022 at 2:52

1 Answer 1

3

You don't need the wrapper with formGroupName="customer" for each form control.

<div class="control" formGroupName="customer">

Change your HTML form by removing formGroupName="customer" as:

<form [formGroup]="customer">
  <div class="control">
    <input type="text" name="name" formControlName="name" />
  </div>
  <div class="control">
    <input type="text" name="name" formControlName="address" />
  </div>
</form>

Demo on Stackblitz

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.