3

I am building a form with complex FormBuilder.

this.myForm = this._fb.group({
        name: ['', [Validators.required, Validators.minLength(5)]],
        addresses: this._fb.group({
          street: ['pp', Validators.required],
          state: {
            city: ['New York']
          },
          postcode: ['']
        })
});

In a nested form, I have a field with can update city. How can I use formControlName for it.

Plunkr

1 Answer 1

3

You just make state as a formGroup, and inside that the form control city:

    this.myForm = this._fb.group({
        name: ['', [Validators.required, Validators.minLength(5)]],
        addresses: this._fb.group({
          street: ['pp', Validators.required],
          state: this._fb.group({
            city: ['New York']
          }),
          postcode: ['']
        })
    });

template:

<div formGroupName="state">
  <input formControlName="city"/>
</div>

Your forked

Plunker

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.