Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add 'required field' asterisk to angular reactive form inputs

I am developing one application with reactive dynamic angular form. This forms fields are coming from an API request and is generated dynamically.

I really need to add 'required field' asterisk (*) to my form inputs which are required. How to achieve this ?

The below is how my form fields look like.

<ng-container *ngIf="input1.type=='string'" [hidden]="False">
 <div>
  <mat-form-field>
   <input matInput   [formControlName]="input1.field_name"  type="text"  placeholder="{{input1.title}}">
  </mat-form-field>   
 </div>
</ng-container>
like image 790
Talk is Cheap Show me Code Avatar asked Apr 23 '18 07:04

Talk is Cheap Show me Code


People also ask

How can we make input field mandatory in AngularJS?

AngularJS ng-required Directive The ng-required directive sets the required attribute of a form field (input or textarea). The form field will be required if the expression inside the ng-required attribute returns true. The ng-required directive is necessary to be able to shift the value between true and false .


2 Answers

Html

<span *ngIf = "input1?.required">*</span>

.ts

You want to require a input depending on some conditions. Have a look here

like image 141
nightElf91 Avatar answered Oct 02 '22 06:10

nightElf91


Should be fixed when Angular v13 is released. https://github.com/angular/components/pull/23362

like image 36
Jadamae77 Avatar answered Oct 02 '22 06:10

Jadamae77