⚠️ This lesson is retired and might contain outdated information.

Group Inputs in Angular 2 Forms with ngModelGroup

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 2 months ago

The ngModelGroup directive allows you to group together related inputs so that you structure the object represented by the form in a useful and predictable way. ngModelGroup is often used in combination with fieldset as they mostly represent the same idea of “grouping together inputs.”

[00:00] To add a password to our form, we must add another input. We'll say the type is password and then the minimal required is saying this is an ngModel, you don't even have to assign it to anything. You do have to give it a name, though. We'll say the name is password.

[00:13] With that information in there, now you'll see when I update the password, it's updating it in the form object, because this form is finding this input with a name of password and saying that password is the value of this here, which is just characters I typed in.

[00:30] If I type in password, you'll see that password shows up here. When I submit as well, you'll see that the object is John, with a password of password. We can group inputs together inside the form. We'll use a field set and wrap out inputs inside of the field set.

[00:47] Tab these in a bit and if we save this as an ngModel group of login, and hit save, we'll now see our object is the root object with a property of login, which has an object of username and password. If I type password again and hit submit, you'll see that submit object has an object for login and that login is an object with a username and password.

[01:12] As you structure out your data and structure out your form, you can use ngModel groups with a property name to help structure out login and password. Then the names of the inputs are going to be the names of the properties inside of that model group.

[01:25] To contrast an input, this is the bare minimum you need, where you need ngModel, the directive on there with a name or pretty much the maximum is an input with a ref to the ngModel, it's own validation, validation rules as well as some default settings for assigning properties into that ngModel.

[01:44] As you build out your forms, you start with the absolute simplest, and then slowly start adding rules, and validators and other visuals to help guide the user.

egghead
egghead
~ 14 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today