Introduction
In this article, I will explain about hiding and showing form fields using Angular forms. Forms are used in web applications that enable the users to enter data and process the data from it.
Generally speaking, Angular forms are of two types, they are Reactive Forms and Template-Driven forms. First of all, the reactive forms are known to be model-driven forms because all the work will be done in component class, whereas template-driven forms are template-based. So, template-driven forms will allow using login forms and functions to work on template-based forms. Here in this article, we will be using template-driven forms along with using the Bootstrap UI library. You can find more information about forms from the following links.
So, coming to the point -- we are going to show/hide form fields. For that, I have created a child component and by using bootstrap library forms I have created a form with two blocks of personal details and communication details.
For creating a new component use the following command to create a new component.
ng g c ComponentName
The ComponentName represents the component name that the user wants to create and I have created a child component named as an employee.
Now open employee.component.html and place the code as represented below.
- <div class="container-fluid">
- <div *ngIf="(showmode=='Register')">
- <form #frmEmployee="ngForm" (ngSubmit)="frmEmployee.form.valid && OnSubmit(frmEmployee)">
- <div class="row" *ngIf="(ObjConfiguration.showpersonalDetails)">
- <div class="col-md-12">
- <strong>
- <h5>Personal Details </h5>
- </strong>
- </div>
- <div class=" col-md-4 form-group" *ngIf="(ObjConfiguration.showname)">
- <label> Name</label>
- <input type="text" class=" form-control text-uppercase" [(ngModel)]="objEmp.PersonalDetails.Name" name="Username"
- id="Username" maxlength="50" #Username="ngModel" required>
- <small class="text-danger" *ngIf="Username.invalid && (Username.touched || frmEmployee.submitted)">Employee Name is required</small>
- </div>
- <div class=" col-md-4 form-group" *ngIf="(ObjConfiguration.showdob)">
- <label>DOB</label>
- <input type="date" class="form-control" [(ngModel)]="objEmp.PersonalDetails.Dob" name="Dob" id="Dob"
- #Dob="ngModel" (blur)="OnDate($event)" required >
- <small class="text-danger" *ngIf="Dob.invalid && (frmEmployee.submitted || Dob.touched)">Dob is required!</small>
- <small class="text-danger" *ngIf="Dob.valid && !IsMinDate">Date should be minimum 2019-01-01</small>
- <small class="text-danger" *ngIf="Dob.valid && !IsMaxDate">Date should be maximum 2025-01-01</small>
- </div>
- <div class="col-md-4 form-group" *ngIf="(ObjConfiguration.showmobile)">
- <label>Mobile</label>
- <input type="text" maxlength="10" class="form-control" [(ngModel)]="objEmp.PersonalDetails.Mobile" name="Mobile" id="Mobile"
- #Mobile="ngModel" pattern="^\d{10}$" required>
- <div *ngIf="Mobile.invalid && (frmEmployee.submitted || Mobile.touched)">
- <small class="text-danger" *ngIf="Mobile.errors.required">Mobile Number is required</small>
- <small class="text-danger" *ngIf="Mobile.errors.pattern">Mobile number must be 10 digits</small>
- </div>
- </div>
- <div class="col-md-4 form-group" *ngIf="(ObjConfiguration.showemail)">
- <label>Email ID</label>
- <input type="email" class="form-control" [(ngModel)]="objEmp.PersonalDetails.Email" name="Email" id="Email"
- #Email="ngModel" pattern="^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$" required>
- <div *ngIf="Email.invalid && (frmEmployee.submitted || Email.touched) ">
- <small class="text-danger" *ngIf="Email.errors.required">Email is required</small>
- <small class="text-danger" *ngIf="Email.errors.pattern">Email must be proper format like @ ,.com</small>
- </div>
- </div>
- <div class="col-md-4 form-group" *ngIf="(ObjConfiguration.showgender )">
- <label>Gender</label>
- <select name="Gender" class="form-control" #Gender="ngModel" [(ngModel)]="objEmp.PersonalDetails.Gender"
- required>
- <option *ngFor="let item of Genders" [value]="item">
- {{item}}
- </option>
- </select>
- <small class="text-danger" *ngIf="Gender.invalid && (Gender.touched || frmEmployee.submitted)">Gender is required</small>
- </div>
- </div>
- <hr class="line">
- <div class="row" *ngIf="(ObjConfiguration.showcommdetails)">
- <h5 class="ml-2 col-md-12 mb-3">
- <strong>Communication Details</strong>
- </h5>
- <div class="col-md-4 form-group" *ngIf="(ObjConfiguration.IsStreet)">
- <label>Street</label>
- <input type="text" class=" form-control text-uppercase" [(ngModel)]="objEmp.CommunicationDetails.Street" name="Street"
- id="Street" maxlength="50" #Street="ngModel" required>
- <small class="text-danger"
- *ngIf="Street.invalid && (Street.touched || frmEmployee.submitted) && ObjConfiguration.IscommMandatory">Street is required</small>
- </div>
- <div class="col-md-4 form-group" *ngIf="(ObjConfiguration.IsArea)" >
- <label>Area</label>
- <input type="text" class=" form-control text-uppercase" [(ngModel)]="objEmp.CommunicationDetails.Area" name="Area" id="Area"
- #Area="ngModel" maxlength="50" required>
- <small class="text-danger" *ngIf="Area.invalid && (Area.touched || frmEmployee.submitted) && ObjConfiguration.IscommMandatory">Area is
- required</small>
- </div>
- <div class="col-md-4 form-group " *ngIf="(ObjConfiguration.IsDistrict)" >
- <label>District</label>
- <input type="text" class="form-control text-uppercase" [(ngModel)]="objEmp.CommunicationDetails.District" name="District"
- id="District" maxlength="50" #District="ngModel" required>
- <small class="text-danger"
- *ngIf="District.invalid && (District.touched || frmEmployee.submitted) && ObjConfiguration.IscommMandatory">District is
- required</small>
- </div>
- <div class="col-md-4 form-group" *ngIf="(ObjConfiguration.IsState)">
- <label>State</label>
- <input type="text" class="form-control text-uppercase" [(ngModel)]="objEmp.CommunicationDetails.State" name="State"
- id="State" maxlength="50" #State="ngModel" required>
- <small class="text-danger" *ngIf="State.invalid && (State.touched || frmEmployee.submitted) && ObjConfiguration.IscommMandatory">State
- is required</small>
- </div>
- <div class="col-md-4 form-group" *ngIf="(ObjConfiguration.IsPincode)" >
- <label>Pincode</label>
- <input type="text" class=" form-control" [(ngModel)]="objEmp.CommunicationDetails.pincode" name="pincode"
- id="pincode" maxlength="6" #pincode="ngModel" required>
- <small class="text-danger"
- *ngIf="pincode.invalid && (pincode.touched || frmEmployee.submitted) && ObjConfiguration.IscommMandatory">pincode is required</small>
- </div>
- </div>
- <div class="text-right m-2 ">
- <button class="btn btn-primary " type="submit">Submit</button>
- <button class="btn btn-danger ml-2" style="float:right" type="reset">Reset</button>
- </div>
- </form>
- </div>
- </div>


Join the conversation! Your thoughts help the community grow.