Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

angular-dynamic-forms

> 编程语言
Open source

Build Forms in AngularJS From Nothing But JSON (please see Alternatives in the README)

376 stars0 likes1 views
WebsiteGitHub

About

Build Forms in AngularJS From Nothing But JSON (please see Alternatives in the README)

angular-dynamic-forms

Build Forms in AngularJS From Nothing But JSON

Uses the MIT License. See LICENSE file for details.

Installation

Bower

The easy way.

  1. bower install angular-dynforms (add --save if you want to add the dependency to your own project - HIGHLY RECOMMENDED)

Git

The old way.

  1. Clone the project from either [GitHub][] or [BitBucket][] - whichever you prefer.
  2. Copy dynamic-forms.js into your project wherever your other assets reside.

Name Change

When registering this project with bower, I discovered that there's another project called [angular-dynamic-forms][wbreza] already registered there. The project was created at the beginning of October 2014, long after this one, and I haven't yet worked out if there are any similarities in the implementation, but as I've been thinking of shortening the name of this project for a while anyway, I went ahead and registered it in bower with the shorter name. I'll be changing the repo name on GitHub and BitBucket, too, but not for several months, to give existing users time to notice the addition of full bower support. The repo will be renamed to match the name registered in bower, and the bower name will not change. It is strongly recommended to use the bower method so you can get the latest version of this project at any given time, regardless of whether I've gotten around to renaming the repo.

Use

As with any other [AngularJS][] module:

  • include the script into your page anywhere after [AngularJS][] itself, using whichever mechanism you use for including scripts in your project:
xml
    
javascript
    require('angular-dynforms');
  • INTERNET EXPLORER: This project (as with most of Angular itself) WILL NOT work properly with IE 6 or 7. Some of the functionality can be coerced into working, but much of it will simply be broken. radio fields, for example, will have every member selected. This may be fixed in a future version, but as it's 2014, IE 6 and 7 are very low priorities, especially with XP reaching end of life. IE 8 will work, with a bit of extra setup (you can try this for IE 6 and 7 as well, but again, they probably won't work):
xml
    
  • inject dynform as a dependency of your project.
javascript
    appModule = angular.module('app', ['dynform']);
  • create a dynamic-form element anywhere in your page.
xml
    
    
  • populate your template with a JSON array describing the form you want to create.
…

And that's about it! Check out the demo for a more robust example, or keep reading to learn about all of the things you can do with this module.

The TL;DR Version

The Directive

You invoke the dynamic-form directive using an element (``) - other options (such as class, attribute, and comment) are unsupported (for now). The directive requires two attributes: an [ng-model][], and either a template or a template-url. The [ng-model][] will be used to generate valid [ng-model][] attributes for the various input controls in the template. In accordance with how [AngularJS][] handles this attribute elsewhere, your entire form's data will be available in keys of whichever model you specify here (though nested forms are an exception, unless you specify a key in the outer form's model as the [ng-model][] of the inner form). You must initialize this parent model to an object, or your app will break.

If you specify a template-url, the dynamic-form directive will retrieve the form template via [$http][] and build out your form based on the response. Currently, failure is silently ignored. This may change in a later release.

You may not want to rely on the directive to retrieve your form directly - perhaps you want to do some processing on the server response before passing it to the directive for building, or maybe you need to specify a more complex [$http][] request with advanced authentication. Or perhaps you just want to proactively handle failure to retrieve the template. Enter the template attribute. When the directive sees template, it ignores any template-url and instead uses the array identified by the template attribute. (See below for more details on this value.) At some point in the future you will also be able to dynamically update this array, and the changes will automatically be reflected in the DOM. This is currently unsupported, however, and for technical reasons, will likely not be supported at all for templateUrl arrays.

Any other attributes you specify on the dynamic-form element are copied across to the form or [ng-form][] element that the directive builds to replace itself with. Similarly, any pre-existing contents are copied across as well, to the top of the resulting form, with the dynamically-specified controls below them. This allows you to nest dynamic-forms inside each other in the same way as [ng-form][] (which is one reason this directive implements this pseudo-transclusion).

The dynamic-form directive makes every attempt to set up the forms it generates to be valid HTML forms, complete with the ability to have their data submitted to the server by the browser's native form submission mechanism and still have the data in the same structure that it takes on in your [AngularJS][] models. This makes it easy to implement a fallback mode in case there is a problem with using the standard [Angular][AngularJS] methods to handle your form inputs. You will, of course, need to provide your own action and method attributes for this to work completely.

The Template

Regardless of whether it arrives via template or template-url, the form template is a fairly-straightforward JavaScript array/object. Each index/key of the template value (referred to elsewhere in this README as an ID) serves as the name and [ng-model][] (where applicable) of the control described in the corresponding value. Each of the values, then, is an object describing a form input control. A type key identifies what kind of control to build, which in turn determines what other keys are expected. Any type that isn't supported builds a `` containing the value of the label key, if one exists, as its content, and any other keys as attributes.

Supported Control Types

Following is a list of all currently-supported types, and then a more detailed specification of each. Links to Angular documentation in the specifications below indicate that values will be added to the Angular-defined attributes mentioned, and that Angular provides the actual functionality described there. Note that not all of these types are properly supported in all browsers, yet; there are a number of references around the web for [which browsers support what][formsupport].

Common Options

  • attributes: key-value pairs for arbitrary attributes not otherwise supported here; it is strongly recommended that you use this option only if the attribute you need isn't already supported, as any attributes specified here bypass any enhancements this module provides.

  • class: see [ng-class][]

  • callback: see [ng-change][] (or [ng-click][] for button-like types)

  • disabled: see [ng-disabled][]

  • label: wraps the control in a `` tag with the value as text content (but see specific types for exceptions to how this is handled)

  • The following options are only supported for types that have values:

    • model: overrides the control's ID as the value of [ng-model][] and name attributes; allows multiple controls to be tied to a single model - you can nest your models further by using dot notation in the value
    • readonly: see [ng-readonly][]
    • required: see [ng-required][]
    • val: an initial value for the model
  • button

  • checkbox

  • checklist

  • color

  • date

  • datetime

  • datetime-local

  • email

  • fieldset

  • file

  • hidden

  • image

  • legend

  • month

  • number

  • password

  • radio

  • range

  • reset

  • search

  • select

  • submit

  • tel

  • text

  • textarea

  • time

  • url

  • week

button

  • Renders: ``
  • Additional Options:
    • None
  • Other Notes:
    • The value of label is used as the content of the `` itself; no additional elements are created

checkbox

  • Renders: ``
  • Additional Options:
    • isOn: see [ng-true-value][]
    • isOff: see [ng-false-value][]
    • slaveTo: see [ng-checked][]
  • Other Notes:
    • See also the checklist type, below

checklist

  • Renders: multiple `` controls
  • Additional Options:
    • options: an object containing a collection of child objects, each describing a checkbox
      • The key of each child object specifies the key to associate with the checkbox it describes
      • class: applies a specific [ng-class][] to the current checkbox, independently of the rest
      • label: operates identically to the standard label option, but applies to a specific checkbox in the list
      • See the checkbox type for other fields supported here
  • Other Notes:
    • This is a convenience type, used to tie a group of checkbox controls together under a single model; the model holds an object, and each control sets a separate key within it
    • You can set a val on the entire checklist (it must, of course, be an object) in addition to any per-option vals; the per-option versions are set after the full checklist version, so they will override anything set to their key by the checklist itself

color

  • Renders: ``
  • Additional Options:
    • None
  • Other Notes:
    • May not be [supported][colorsupport] in all browsers

date

  • Renders: ``
  • Additional Options:
    • See text below
  • Other Notes:
    • May not be [supported][datesupport] in all browsers

datetime

  • Renders: ``
  • Additional Options:
    • See text below
  • Other Notes:
    • May not be [supported][datesupport] in all browsers

datetime-local

  • Renders: ``
  • Additional Options:
    • See text below
  • Other Notes:
    • May not be [supported][datesupport] in all browsers

email

  • Renders: ``
  • Additional Options:
    • See text below
  • Other Notes:
    • On devices that have on-screen keyboards, the browser may modify the keyboard layout to make entering email addresses in these controls easier.

fieldset

  • Renders: ``
  • Additional Options:
    • fields: the template for the fields which should appear in the fieldset
  • Other Notes:
    • The value of label is used to create a `` tag as the first child of the fieldset

file

  • Renders: ``
  • Additional Options:
    • multiple: whether or not the user can select more than one file at a time with this single control
  • Other Notes:
    • [A directive][filedirective] is included with this module that allows file controls to properly bind to [AngularJS][] models - the control's FileList object is stored in the model, and updating the model's value with a valid FileList object will update the control accordingly
    • Also included is [an AngularJS service][fileservice] that wraps the browser's FileReader in a promise, so you can get the contents of the selected file for furth

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言