This tutorial is out of date and no longer maintained.
In this tutorial we will explore the way to bind these few types of controls to our form: text, number, radio, select (primitive type), select (object), multiple select, checkbox (boolean), and checkbox (toggle value).
Feel free to skip some of the control types (as some of them are really simple).
If you are new to Angular 2 forms, do refer to these articles for basics.
View Angular 2 - Different form controls (final) scotch on plnkr
We will build a form to capture user information based on these interfaces.
Here is how the UI will look:
Here’s our file structure:
|- app/
|- app.component.html
|- app.component.ts
|- app.module.ts
|- main.ts
|- theme.interface.ts
|- user.interface.ts
|- index.html
|- styles.css
|- tsconfig.json
In order to use the forms module, we need to npm install @angular/forms
npm package and import the forms module in the application module.
Here’s the module for our application app.module.ts
:
Let’s move on to create our app component.
We need to include some data for setup as well:
Then, we need to initialize our user model:
This is how our HTML view will look like.
Let’s start to look into each type of control.
Getting text input is very straightforward. You need the name
attribute, and ngModel
.
Getting number input is also very straightforward.
Binding radio input is not that easy prior Angular RC 2. With the new form in RC 3 onward, we can directly bind to ngModel
, bind the value
property.
We have this list of preferred languages:
When select, we want only the value Hammerhead or Great White Shark.
You can bind select to ngModel
. Loop through your option
list, set the value
property.
We have a list of roles:
When value selected, we expected it to return string value admin, guest, or custom. Here’s how your HTML will look like.
Similar to the last example, but this time, instead of a simple type, we want the whole object when it’s selected.
Here is the list of themes:
When selected, for example Light theme, we expect { backgroundColor: 'white', fontColor: 'black', display: 'Light' }
to be returned. Instead of binding to the value property, we bind to ngValue
property.
We can select more than 1 topics. E.g. when selecting game and tech, it should return ['game', 'tech']
.
Similar to select, but this time our model is array of string.
By default, checkboxes return boolean. Bind ngModel
and define the name attribute as usual.
In this case, we want to display a checkbox. Instead of boolean, we expecting value. When checked, it should return toggled, else return value untoggled.
This is the list of toggles:
First, we define a hidden input to bind to the real model. Then we create the checkbox input, handle the checked
property and change
event. Change event fire every time value change, and it has $event
that we can read from.
In our case, we read the $event.target.checked
to find out if the checkbox is checked, then update model value accordingly.
During development, it’s good that you can visualize the value. Angular provided a very useful json
Pipe.
That’s it. Hope it helps your journey in Angular 2. Happy coding!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!