I'm a newbie with Angular2 (beta1) and I'd like to implement a sort of simple editable grid, built of 2 components. Here I use two fake-data components to keep things simple. They are (see this Plunker: http://plnkr.co/edit/5cZfLTIlhLc82wWV4PQI):
- the parent component, named
contact
. Say it represents a contact with a name. - the child component, named
entry
. Say it represents an entry for a contact, where each contact can include 0 or more entries. Each entry has an address and a zip code.
I'd like to create a form where the user can edit the contact's properties, and also its children entries: he could add a new entry, delete an existing entry, or edit an existing entry.
To this end, the views for both these components provide a form-based template. I can think of this data flow:
contact: the user edits the form and then clicks a submit button to save the whole thing. Thus, I can just have some code handling the submit button and emitting an event as the component output. The contact has an
entries
array property: I can thus use anngFor
directive in its template to render an entry component for each of them.entry: the entry has properties
addressCtl
andzipCtl
which represent the control directives included in theControlGroup
representing the whole form. Also, I need a couple of properties to be bound as the input of the component (address
andzip
), so that in the parent template I can do something like:<tr *ngFor="#e of entries"> <td><my-entry [address]="e.address" [zip]="e.zip"></my-entry></td> </tr>
Now, it's not clear to me how to shape the relation between the "model" properties representing the control's input, and the "form" directives properties. I should be able to get the address and zip values from the parent component through the [...]
binding, and pass the updated values up through an event fired by the child component (e.g. blur?). Does this make sense in the NG2 world? Anyway, I'm missing a piece here: how can I connect the form controls values to the model properties values? Could anyone make this clearer or point to some good docs?