2017-11-21 15:27:57
JavaScript
, 2017-11-21 15:27:57

Angular2 how to write input data to sub-element?

To make it clearer.
5a14198e6ca54130840825.png
There is a form. I am getting this form data. formGroup looks like this (pay attention to the contacts object, in the second case they are)

this.createForm = this.fb.group({
    username: [''],
    password: [''],
    firstName: [''],
    lastName: [''],
    patronymic_name: [''],
    birthday: [''],
    gender: [''],
    taxation_id: [''],
    appointment: [''],
    snils: [''],
    telephones: ['', []],
    emails: ['', []],
    postals: ['', []]
});

And you need the formGroup structure to look like this
this.createForm = this.fb.group({
    username: [''],
    password: [''],
    firstName: [''],
    lastName: [''],
    patronymic_name: [''],
    birthday: [''],
    gender: [''],
    taxation_id: [''],
    appointment: [''],
    snils: [''],
    contacts: {
        telephones: ['', []],
        emails: ['', []],
        postals: ['', []]
    }
});

How can this be done?
And some HTML
<form [formGroup]="createForm">
    <section>
        <label class="input">
            <span>Email:</span>
            <i class="icon-append fa fa-unlock-alt"></i>
            <input type="text" name="firstName" placeholder="" formControlName="emails">
            <b class="tooltip tooltip-bottom-right">{{'Needed to verify your account' | i18n }}</b>
        </label>
    </section>
    <section>
        <label class="input">
            <span>Адрес проживания:</span>
            <i class="icon-append fa fa-unlock-alt"></i>
            <input type="text" name="firstName" placeholder="" formControlName="postals">
            <b class="tooltip tooltip-bottom-right">{{'Needed to verify your account' | i18n }}</b>
        </label>
     </section>
</form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sasha Novik, 2017-11-26
react

A field inside a FormGroup can be either a FormControl or another FormGroup.
And then the form build will look like this:

this.createForm = this.fb.group({
    ...
    contacts: this.fb.group({
        telephones: ['', []],
        emails: ['', []],
        postals: ['', []]
    })
});

HTML:
<form [formGroup]="createForm">
   <div formGroupName="contacts">
      ...
     <input type="text" name="firstName" placeholder="" formControlName="emails">
     ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question