3

What is the best way to go about creating a small bit of HTML Form elements and reusing that as a snippet multiple times in a form, but with different models?

e.g. inputs for firstName, lastName, streetAddress, city, state, zip for student, parent, pointOfContact

If I was hard-coding the model and inputs, I would have something like:

<input name="student.firstName" ng-model="student.firstName" >
<input name="student.lastName" ng-model="student.lastName" >
<input name="parent.firstName" ng-model="parent.firstName" >
<input name="parent.lastName" ng-model="parent.lastName" >
<input name="pointOfContact.firstName" ng-model="pointOfContact.firstName" >
<input name="pointOfContact.lastName" ng-model="pointOfContact.lastName" >

As a angularJs noob, I'm not sure how to do $modelPrefix.firstName

Template ?

<input name="$modelPrefix.firstName" ng-model="$modelPrefix.firstName" >
<input name="$modelPrefix.lastName" ng-model="$modelPrefix.lastName" >
1
  • I'm not clear on what you need. If you have multiple input elements and you reuse them in some way, are you really saving any code? Can you provide the markup you're trying to reduce? Commented Apr 6, 2013 at 2:37

2 Answers 2

3

Here is an example of how to re-use a set of form fields using a directive.

First create an html template file with the common fields (address.html):

<div>
  <label for="line1-id">Address</label>
  <input id="line1-id" name="line1" type="text" ng-model="address.line1" required ng-maxlength="255" />
</div>
<div>
  <label for="city-id">City</label>
  <input id="city-id" name="city" type="text" ng-model="address.city" required ng-maxlength="255" />
</div>
<div>
  <label for="state-id">State</label>
  <input id="state-id" name="state" type="text" ng-model="address.state" required ng-maxlength="255" />
</div>
<div>
  <label for="postalCode-id">Postal code</label>
  <input id="postalCode-id" name="postalCode" type="text" ng-model="address.postalCode" required size="5" ng-maxlength="5" ng-minLength="5"/>
</div>

Then create a directive that references this template (address.js):

(function() { 'use strict';

  angular.module('myModule').directive('myAddress', address);

  function address() {
    var directive = {
      scope: {
        form: '=form',
        address: '=address'
      },
      restrict: 'E',
      templateUrl: 'address.html'
    };

    return directive;
  }

}());

Then you can use this directive in your form:

<form name="ctrl.myForm" ng-submit="ctrl.save()">
  <!-- other form fields -->
  <my-address form="ctrl.myForm" address="ctrl.employee.address"></my-address>
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

Upvoting for the sample provided.
2

Wrap each of the reusable HTML components in an Angular Directive. This would allow you to reuse the HTML while still passing in different models to each.

This technique (and others) are explained really will in the videos posted on http://egghead.io/ For your purposes pay close attention to videos 10,11,14 and 15.

2 Comments

I'm pretty sure I understand what you mean, but could you provide an example of both the directive and usage of the directive? I'm pretty sure I need scope:true in the directive, but I'm not sure what else I need to look out for.
do you proposed a pay solution?

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.