Guides
GuidesExamplesGitterLog In
Guides

Custom Templates

angular-formly types for CSS frameworks

You can create your own templates with formlyConfig in your run function (or formlyConfigProvider in a config function). This is the recommended approach if you want to customize your templates at all.

Prebuilt Templates

While it is recommended to create your own templates for ultimate customization and flexibility, there are prebuilt templates you can use:

Creating a Custom Template

Creating a custom template is quite easy, but also very flexible. It can be as simple as:

formlyConfig.setType({
  name: 'input',
  template: '<input ng-model="model[options.key]" />'
});

or as complex as

formlyConfig.setType({
  name: 'ipAddress',
  extends: 'input',
  wrapper: 'someWrapper',
  defaultOptions: {},
  link: function(scope, el, attrs) {},
  controller: function($scope, yourInjectableThing) {},
  apiCheck: {}
  // and more...
});

Then someone would use your type like so:

// in a controller
this.fields = [
  {
    type: 'ipAddress',
    // other stuff
  }
];

You have a lot of freedom when it comes to writing templates. You don't even need to use the model which means that you can have fields that are just part of the look and feel of your form. Because of angular-formly's ngModelAttrsTemplateManipulator, you really only have to put the ng-model element where you want it (if you want it at all) and formly will take care of most of the rest. If you want to have any extra properties, you have pretty much full reign over the options.templateOptions object. Just be aware that angular-formly does make use of common parameters like required or onClick to automatically add attributes to ng-model elements. See formlyConfig for more information.

formlyConfig.setType options

OptionData Type(s)Description
namestringHow you reference the type for a specific field.
templatestringInlined template for the type. Can only be specified if there is no templateUrl
templateUrlstringThe URL for a template (uses the $templateCache). Can only be specified if there is no template.
defaultOptionsobject (must be valid field config)This will be merged with the options the user of the type provides (so they can override these options).
wrapperstring || array of stringsThe name of a wrapper that has been registered with angular-formly
controllercontroller name || controller functionProvides you the ability to add custom behavior to the type without having to make an entire directive (you can make a directive instead if you wish).
linkfunction(scope, el, attrs)Provides the ability to manipulate the DOM of the field
apiCheckobject of functionsangular-formly uses apiCheck for validating options. You can enforce the API to your type this way.
apiCheckInstanceInstance of apiCheckYour own instance of apiCheck so you have the correct prefix/suffix/url/etc.
apiCheckFunction'throw' or 'warn'Defaults to 'warn'. If you specify 'throw' then an error will be thrown instead of 'warn'
apiCheckOptionsobjectThe options to use when the check fails. Defaults to something sensible.
validateOptionsfunction(options)Note: It is recommended that you use apiCheck with apiCheckInstance rather than validateOptions.

This function will be invoked with the options of the field after it has been merged with it's optionsDefaults and
any types that its type extends. Feel free to log warnings to the console or throw errors here to help users use your
types correctly. Recommended: Use apiCheck.js as this is what formly uses
and will already be available to you.

Template's $scope

See this example for an example what's accessible on the template's scope for your custom types.

Here's what you have available on the $scope of your templates:

Variable NameDescription
optionsThe data provided to configure the field. As a template author, if you need custom properties, you use the templateOptions property on this object. Note, there are a few things that get added to your field configuration (and hence, these options). One of these is "formControl" which can be used to display errors to the user (for example).
toShortcut to options.templateOptions
fcShortcut to options.formControl
showErrorShortcut to options.validation.errorExistsAndShouldBeVisible
idThe id of the field. You shouldn't have to use this.
indexThe index of the field the form is on (in ng-repeat)
formthe form controller the field is in
modelthe model of the form (or the model specified by the field if it was specified).
fieldsall the fields for the form
formStateThe object passed as options.formState to the formly-form directive. Use this to share state between fields.

Sponsor