In this exercise we're going to build a template-driven form to add a new contact to our contacts list using Angular form directives.
We can list and edit our contacts, but we don't have a way to add new ones. That's why we want to create a new form component that enables us to do that.
-
Create a new component ContactsCreatorComponent using angular-cli
$ ng generate component contacts-creator
or
$ ng g c contacts-creator
-
Render a form to add a new contact using the following HTML:
<div class="trm-contacts-creator">
<form>
<mat-card>
<mat-card-title-group>
<img mat-card-md-image alt="Placeholder image" src="/assets/images/placeholder.png">
<mat-card-title></mat-card-title>
<mat-card-subtitle></mat-card-subtitle>
</mat-card-title-group>
<mat-card-content>
<div fxLayout="column">
<mat-input-container fxFlex>
<input matInput placeholder="Name" name="name">
</mat-input-container>
<mat-input-container fxFlex>
<input matInput placeholder="Email" name="email">
</mat-input-container>
<mat-input-container fxFlex>
<input matInput placeholder="Birthday" name="birthday" type="date">
</mat-input-container>
<mat-input-container fxFlex>
<input matInput placeholder="Phone" name="phone">
</mat-input-container>
<mat-input-container fxFlex>
<input matInput placeholder="Website" name="website">
</mat-input-container>
<mat-radio-group name="gender">
<mat-radio-button>
INSERT_GENDER_VARIANT
</mat-radio-button>
</mat-radio-group>
<fieldset fxLayout="column">
<legend>Address</legend>
<mat-input-container fxFlex>
<input matInput placeholder="Street" name="street">
</mat-input-container>
<mat-input-container fxFlex>
<input matInput placeholder="Zip" name="zip">
</mat-input-container>
<mat-input-container fxFlex>
<input matInput placeholder="City" name="city">
</mat-input-container>
<mat-select placeholder="Country" name="country">
<mat-option>INSER_COUNTRY_NAME</mat-option>
</mat-select>
</fieldset>
</div>
</mat-card-content>
<mat-card-actions fxLayout fxLayoutAlign="center center">
<button mat-button type="submit">Save</button>
<a mat-button title="Cancel creating new contact">Cancel</a>
</mat-card-actions>
</mat-card>
</form>
</div>
- Add a new route configuration that loads
ContactCreatorComponent
with the pathcontact/new
(keep in mind that it has to be defined beforecontact/:id
route) - Append the following HTML to the
ContactsList
template, to create a button that links toContactCreatorComponent
:
<a routerLink="/contact/new" mat-fab title="Add a new contact" class="trm-floating-button">
<mat-icon class="md-24">add</mat-icon>
</a>
- Add a new method
addContact(contact: Contact)
to ContactsService that performs and http POST request tohttp://localhost:4201/api/contacts
with a new contact data object that is passed to this method (you can useupdateContact()
as inspiration). - Add
ngModel
andngModelGroup
to ContactCreatorComponents template accordingly to submit an object structure on save that looks like this:
{
name: '',
email: '',
phone: '',
birthday: '',
website: '',
address: {
street: '',
zip: '',
city: '',
}
}
- Get an
ngForm
reference in the template and use it's value property to submit data on save ((ngSubmit)="save(form.value)"
). - Add a new method
save(contact:Contact)
to ContactCreatorComponent that callsContactsService#addContact()
with the given contact, and navigates to ContactsListComponent, once the method returns.
Note: if the
save(contact)
generates a server 404 response, ensure that the contact data submitted to the server is valid.