GuidesExamplesGitterLog In

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:

  name: 'input',
  template: '<input ng-model="model[options.key]" />'

or as complex as

  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


Data Type(s)




How you reference the type for a specific field.



Inlined template for the type. Can only be specified if there is no templateUrl



The URL for a template (uses the $templateCache). Can only be specified if there is no template.


object (must be valid field config)

This will be merged with the options the user of the type provides (so they can override these options).


string || array of strings

The name of a wrapper that has been registered with angular-formly


controller name || controller function

Provides 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).


function(scope, el, attrs)

Provides the ability to manipulate the DOM of the field


object of functions

angular-formly uses apiCheck for validating options. You can enforce the API to your type this way.


Instance of apiCheck

Your own instance of apiCheck so you have the correct prefix/suffix/url/etc.


'throw' or 'warn'

Defaults to 'warn'. If you specify 'throw' then an error will be thrown instead of 'warn'



The options to use when the check fails. Defaults to something sensible.



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 Name



The 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).


Shortcut to options.templateOptions


Shortcut to options.formControl


Shortcut to options.validation.errorExistsAndShouldBeVisible


The id of the field. You shouldn't have to use this.


The index of the field the form is on (in ng-repeat)


the form controller the field is in


the model of the form (or the model specified by the field if it was specified).


all the fields for the form


The object passed as options.formState to the formly-form directive. Use this to share state between fields.

Did this page help you?