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 1046 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.

angular-formly: Extending Types

2:17 Angular 1.x lesson by

Extending types is one of the ways that makes angular-formly help you keep your Angular forms DRY. When use responsibly, they can make it much easier to manage common types, allowing you to add a bit of functionality to a common type.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Extending types is one of the ways that makes angular-formly help you keep your Angular forms DRY. When use responsibly, they can make it much easier to manage common types, allowing you to add a bit of functionality to a common type.

Angular-formly allows you to extend existing types. We have a list of Avengers here. We can choose our favorite. I'll say, "Captain America." That will set our favorite Avenger. Everything is working as we would expect.

Let's say that we wanted to use this avengerSelect in many places throughout our application. We wouldn't want to have to copy these options everywhere. We're going to create an avengerSelect type that will extend the select type.

This will happen inside of our run phase. We'll say, "function FormlyConfig," and then we'll simply say, "FormlyConfig.setType." Our type name will be "avengerSelect." This will extend select.

Now we can specify some default options. We'll go ahead and copy all of these inside of our default options. Instead of select here, we're going to use avengerSelect. Now everything else works as expected.

We've moved all of our common configuration inside of this avengerSelect. Everything works exactly as it did before, but now we can use this throughout our application just by saying the type is avengerSelect.

Obviously, this will be a favorite Avenger too. They won't be bound directly to the same model in that case. Also, you can override these default options as well, by simply specifying the option. We'll say, "templateOptions with label favorite", we'll say, "Second Favorite.'"

You can override these different options as well. This makes your code a lot more dry and quite readable as well. You simply say, "The type is avengerSelect." I know exactly what that's going to look like. I know the options that it's going to have.

That is how you extend types using angular-formly. Inside of your run phase, you inject the FormlyConfig, and then you set the type. You give it a name, and then this key there is the extends select.

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