Angular2- Template driven form validation for input field in child component

后端 未结 1 1286
花落未央
花落未央 2021-01-26 12:51

I was struggling with adding template driven form validation in my angular2 app.

i have a forked the plunker https://plnkr.co/edit/phhe74kAUmNZgNSmcsvm?p=pr

相关标签:
1条回答
  • 2021-01-26 13:16

    Currently, your form doesn't recognize your child components' input field as a form field, you need to change your child tag a bit... to have the name attribute and ngModel to bind this child component to the form. Also include required here.

    Lastly, you need to add ngDefaultControl to have two-way binding for the form field. So your child tag should look like this in your parent:

    <my-input name="postcode" ngModel ngDefaultControl required [(value)]="user.address.postcode"></my-input>
    

    Here is your forked

    Plunker

    0 讨论(0)
提交回复
热议问题