We'll be creating a shared/reusable component in Angular 4 (works in Angular 2+ as well).
The reason I needed a shared Address component was because I was tired of repeating the Pick a state dropdown, and I always wanted my city and state to sit next to each other using flex. Seemed like a good excuse to try to create a shared component.
One other thing to consider was that I needed this component in a variety of forms:
- Profile Address change
- Admin user management
Creating the component
If you're using the Angular CLI the easiest thing is just to type
ng g component address. This creates the following four files:
Let's start with the Typescript file, as there is very little to add to this file. We're going to add three items to the AddressComponent class before the constructor:
In order for Typescript to compile, don't forget to change the import statements at the top of the file:
Creating the template
Now that we have our component setup, let's create a template to accept the FormGroup instance and the list of states.
The reason I've chosen a div here instead of a form element is because this element will only ever live inside a form already. If you want to get to know more about the FormGroup in Angular I'd highly suggest reading the Official Angular docs on FormGroup. But let's move on to some form inputs.
There's nothing crazy about that html, but take note of a couple of things:
- use formControlName to reference variables that will be stored in the FormGroup.
- note the *ngFor that iterates through the list of states
- city and state are both under the same .form-group div for styling later
Style the component
There's not much to styling this component, but I'll just use a tiny bit of flex to make sure that city and state sit next to each other and look nice. I also use a base class for a standard 8px grid. I also set the zip input to be a set width, since that will have a max of either 5 or 10 (with a hyphen) digits.
Share the component
To use this in both the internal (admin) and external (registration) portions of my app I have created a shared module. I think I've covered this in a previous post, but just in case, here's the whole file (since I only have 1 shared component at the moment):
Note that FormsModule and ReactiveFormsModule are necessary for everything to work.
Inject the component
In my external.module, which handles all the things on the outside of my app, here's what I have (I've stripped irrelevant things like routing for the code sample):
So now my RegisterComponent has access to anything the ShareModule has exported (ie, the AddressComponent).
Using the shared component
Now in register.component.ts I need to create a new form. I'm going to be using the FormGroup and the FormBuilder. I'll post the whole code for now and explain after:
So, right away you should notice that I've imported FormGroup and FormBuilder. These are great because they make it really easy to construct forms. I'm registering registerForm as a variable on the class to be assigned later.
Down in the createForm method you can see that I'm assigning a new FormGroup using the FormBuilder. You can also see that the address portion is its own FormGroup. This is so we can pass it straight into the Address Component, which is expecting a FormGroup as an @Input.
Setup the registration form HTML
Hopefully that HTML is pretty self-explanatory. A few things to note:
- Using formControlName again to add in the fields the FormGroup expects
- Passing registerForm.controls.address to the myapp-address component's group input attribute.
- Setting the submit button to disabled when the form is not valid.
So go ahead and spin up the form and you should see a city and state next to each other, a zip field that is a set width, and when you submit the form you should see the entire form's value logged to the console.
There are a few things about this I'm curious about.
I'm not really clear on how the binding is working between the registerForm and the address FormGroup I'm passing into the shared component. It feels an awful lot like 2-way binding, which I thought Angular was moving away from.
I have not added any validators in this tutorial, but it seems quite straight forward when you read the Angular docs on the subject.
This approach seems maybe a little overkill (or maybe I didn't include enough fields), but I mostly didn't want to repeat that states array in all of the places I had user forms. I could probably move more of the user info into the component, but I like how this component functions for now.
You could also add city/state/zip validation to make it even easier for your users.
Hope this helps your project. I'm learning Angular 4 myself and posting stuff here as I go, leave comments if something looks off to you.