[Angular2 Form] Nested Formgroup, and usage of formgroupname

Source: Internet
Author: User

We can nest Formgorup:

    this. Reactiveform = fb.group ({      username: [        '],        [          validators.required,          validators.minlength (3)        ]      ,      pwds:fb.group ({         "' ,         "'       }, {validator:passwordvalidator})    });

We make password as an own group. So in HTML, we need to use formgroupname istead of Formcontrolname.

<form[Formgroup]= "Reactiveform"novalidate AutoComplete= "Off">  <Divclass= "Form-field">    <label>Username:</label>    <inputFormcontrolname= "username">    <Divclass= "Field-error-message"*ngif= "ReactiveForm.controls.title.errors?". Required ">Username is required</Div>  </Div>  <DivFormgroupname= "PWDs">    <Divclass= "Form-field">      <label>Pwd</label>      <inputFormcontrolname= "pwd">    </Div>    <Divclass= "Form-field">      <label>Rpwd</label>      <inputFormcontrolname= "Rpwd">    </Div>  </Div></form>

And how do we check the value or errors?:

< Pre >   {{reactiveform.get (' PWDs ')?. Value | JSON}}  {{reactiveform.get (' PWDs ')?. Errors | JSON}}</pre>

And we also Passwordvalidator Haven ' t cover yet, it is just a fucntion:

function Passwordvalidator (c:abstractcontrol) {  return c.get('pwd  '). Value = = c.Get('rpwd'). Value?    NULL // valid    // Invalid      true     }}

And notice that we put this validator inside the nested group, so we can get nice error effect:

[Angular2 Form] Nested formgroup, and usage of formgroupname

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.