Nesting Form Data

If you find yourself wrestling to fit nested trees of data inside of a flat form, Angular has you covered for both simple and complex cases.

Let's assume you had a payment endpoint which required data, similar to the following:

  1. {
  2. "contact": {
  3. "firstname": "Bob",
  4. "lastname": "McKenzie",
  5. "email": "BobAndDoug@GreatWhiteNorth.com",
  6. "phone": "555-TAKE-OFF"
  7. },
  8. "address": {
  9. "street": "123 Some St",
  10. "city": "Toronto",
  11. "region": "ON",
  12. "country": "CA",
  13. "code": "H0H 0H0"
  14. },
  15. "paymentCard": {
  16. "provider": "Credit Lending Company Inc",
  17. "cardholder": "Doug McKenzie",
  18. "number": "123 456 789 012",
  19. "verification": "321",
  20. "expiry": "2020-02"
  21. }
  22. }

While forms are flat and one-dimensional, the data built from them is not. This leads to complex transforms to convert the data you’ve been given into the shape you need.

Worse, in cases where it is possible to run into naming collisions in form inputs, you might find yourself using long and awkward names for semantic purposes.

  1. <form>
  2. <fieldset>
  3. <legend>Contact</legend>
  4. <label for="contact_first-name">First Name</label>
  5. <input type="text" name="contact_first-name" id="contact_first-name">
  6. <label for="contact_last-name">Last Name</label>
  7. <input type="text" name="contact_last-name" id="contact_last-name">
  8. <label for="contact_email">Email</label>
  9. <input type="email" name="contact_email" id="contact_email">
  10. <label for="contact_phone">Phone</label>
  11. <input type="text" name="contact_phone" id="contact_phone">
  12. </fieldset>
  13. <!-- ... -->
  14. </form>

A form handler would have to convert that data into a form that your API expects. Thankfully, this is something Angular has a solution for.

ngModelGroup

When building a template-driven form in Angular, we can lean on the ngModelGroup directive to arrive at a cleaner implementation, while Angular does the heavy lifting of converting form-fields into nested data.

  1. <form #paymentForm="ngForm" (ngSubmit)="purchase(paymentForm)">
  2. <fieldset ngModelGroup="contact">
  3. <legend>Contact</legend>
  4. <label>
  5. First Name <input type="text" name="firstname" ngModel>
  6. </label>
  7. <label>
  8. Last Name <input type="text" name="lastname" ngModel>
  9. </label>
  10. <label>
  11. Email <input type="email" name="email" ngModel>
  12. </label>
  13. <label>
  14. Phone <input type="text" name="phone" ngModel>
  15. </label>
  16. </fieldset>
  17. <fieldset ngModelGroup="address">
  18. <!-- ... -->
  19. </fieldset>
  20. <fieldset ngModelGroup="paymentCard">
  21. <!-- ... -->
  22. </fieldset>
  23. </form>
- Using the alternative HTML5 labeling format; IDs have no bearing on the ngForm / ngModel paradigm- Aside from semantic purposes, ngModelGroup does not have to be used on
— it would work just as well on a
.

If we were to fill out the form, it would end up in the shape we need for our API, and we can still rely on the HTML field validation if we know it’s available.

原文: https://angular-2-training-book.rangle.io/handout/forms/template-driven/nested-form-data.html