Angular How to make [formControl] an optional field

Viewed 8125

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?

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],
Related