Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 916 of the free egghead.io lessons, plus get Angular 1.x content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



angular-formly: Custom Templates

5:05 Angular 1.x lesson by

The type functionality of the angular-formly library is powerful. In this lesson, see how you can use it to create custom templates for your field configurations and see what is available in the scope of each of your fields.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

The type functionality of the angular-formly library is powerful. In this lesson, see how you can use it to create custom templates for your field configurations and see what is available in the scope of each of your fields.

To create a custom template using Angular-Formly, you need to register a type with Angular-Formly. We'll do this during the run phase, say app.run function formlyConfig. Then in our formlyConfig, we'll say set type, and we'll pass an object. This is our configuration for this type.

The type will have a name, and this will be input and then we'll also provide a template and we'll just say helloworld. Inside of our field configuration, we'll specify a type as input...Oh and I've not got a semi-colon there, there we go.

Then we'll see that our field there is "Hello World." Now, let's make this a little more interesting and let's look at some of the options that you have with your templates, your custom templates. Instead of templates, we're going to specify a template URL. I have one already in here called input.html.

Now, let's look at what that template looks like. What I have here is a pre that is [indecipherable 0:00:56] repeating over this, which is the scope, and then displaying the value of each of its properties.

Let's uncomment this, and see what we have available to us inside of our template. You have available to you all of the fields for that particular form. You will not likely use this very often, but this can be useful sometimes. You also have access to the form controller for the current Formly form that this field is being used in. You have a form ID, you will not use this very often.

You have form state, and this comes from the options that you pass in, so if I were to pass in on the Formly form options, say vm.options, and then in here I say vm.options is an object that has formstate, and then I say foobar here, then our formstate is foobar.

This is a way that you can share state between different fields inside of the same form. Then you have your form ID, or rather the ID of the field and you won't use that very much, Angular Formly uses this internally.

Then the index of the field, and the model, so if I were to pre-fill this with first name is Jane, then in here, we see that our model is firstname Jane. Then also the options, which represents all of these options here, but you'll notice there are a few extra things that are attached to those options. These are some things that Angular Formly tacks on.

Finally, you'll have access to the TO object, which is actually a shortcut to template options, and we'll say label = firstname.

Now, in here, inside of our TO, we see label is firstname. It's a shortcut just because it's a lot more work to say options.templateoptions.label than it is to simply say TO.label.

One other thing that you don't see in here comes if you have an ng model in your template.

Let's add an input with an ng model. If we look at the different things that we have available to us, we have the model and that's what we want to bind this to, and then we say options.key, and this is what you'll normally want for all of your templates is this is the model that the user of your template is trying to specify as what they want this field to be bound to, is whatever the model is is specified and then the key on that model.

Now, inside of here, we also have fc which standard for form control. This fc is actually simply a shortcut to something that is placed onto our options called form control. So it's options.formcontrol, and this is the form control for this particular field. It's basically the ng model controller for this field.

One other thing is this show error, this will indicate to you whether or not you should be showing any error messages based on the validity of the form.

If I were to in here say required is true, that show error will say false because the field hasn't been touched. Once I touch it and then blur, then you'll notice untouched get sets to false, and touched get sets to true. We'll go back down to our show error and that is now true.

In review, to create a custom template, you simply set a type with a name and a template URL or a template, and inside of the template, you have access to several things that tell you information about the configuration for that field, and you use those configurations to render whatever it is that you want to for that field.


Featured course:

Create an SVG Icon System

Create an SVG Icon System

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?