Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular How to make [formControl] an optional field

In Angular you can add attributes to inputs and make them optional using attr:

<input [type]="text" [attr.id]="id">

But how do you make a form control optional? I want something like this:

<input [type]="text" [attr.formControl]="someControl">

Where the formControl will not be set if someControl is undefined or null?

like image 822
HsuanWei Fu Avatar asked May 18 '17 07:05

HsuanWei Fu


1 Answers

As long you do not attach a required Validator to a formControl it is considered optional in the form. Your form will not have any error for such fields since there is no required Validator attached to them. It is attached like this:

firstName: ['', Validators.required],
like image 150
nircraft Avatar answered Oct 15 '22 12:10

nircraft