In this column we are activity to see how to dynamically actualize a acknowledging anatomy with configurable fields and validations.
Dynamic anatomy can be actual advantageous and added economical to actualize the forms based on capricious business article archetypal and after the charge for adding/changing any code.
Install Angular CLI latest adaptation if you don’t acquire the latest adaptation of the CLI.
Create a new activity as dynamic-form.
It takes a brace of account for creating a project. Once the activity is created, change the accepted agenda into dynamic-form.
Install Angular Material. Since we are activity to use the angular actual UI components.
We will use the ng add command to add Angular Actual to the application.
Install @angular/material-moment-adapter and moment. It’s the annex for Actual Datepicker.
Create a custom bore as material.
Import the appropriate actual modules by pasting the beneath cipher in /src/app/material.module.ts file.
Import MaterialModule, FormsModule, ReactiveFormsModule in /src/app/app.module.ts file.
Let’s actualize the interfaces for activating acreage configuration. Actualize a book alleged field.interface.ts central the app agenda and adhesive the beneath code.
First, we will actualize the apparatus for anniversary blazon of fields (Input, Button, Select, Datepicker, Radioon, Checkbox) appliance the actual components. All the apparatus acquire two inputs FieldConfig (name, type, label, value, validations,. .) and FormGroup.
Create a agenda apparatus central the app directory.
Create an ascribe basic beneath app/components agenda and acceptation it to app.module.ts.
Paste the beneath cipher in /app/components/input/input.component.ts file. It accepts two inputs acreage blazon of FieldConfig and accumulation blazon of FormGroup. In the template, bind the formGroup, formControlName and placeholder directives and use the <mat-error> aspect to affectation the validation letters if the agnate acreage validation altitude are not satisfied.
Create a on basic beneath app/components agenda and acceptation it to app.module.ts.
Paste the beneath cipher in /app/components/on/on.component.ts file.
Create a baddest basic beneath app/components agenda and acceptation it to app.module.ts.
Paste the beneath cipher in /app/components/select/select.component.ts file.
Create a date basic beneath app/components agenda and acceptation it in app.module.ts to add actual datepicker basic functionality.
Paste the beneath cipher in /app/components/date/date.component.ts file.
Create a radioon basic beneath app/components agenda and acceptation it to app.module.ts.
Paste the beneath cipher in /app/components/radioon/radioon.component.ts file.
Create a checkbox basic beneath app/components agenda and acceptation it to app.module.ts.
Paste the beneath cipher in /app/components/checkbox/checkbox.component.ts file.
All the fields apparatus are ready. Now we will actualize a dynamicfield charge to dynamically actualize the InputComponent, ButtonComponent, SelectComponent, DateComponent and RadioonComponent, CheckboxComponent.
Create a agenda dynamic-field central the app/components/ directory.
Create a charge dynamic-field beneath app/components/dynamic-field agenda and acceptation it to app.module.ts.
Now we can add all the necessory cipher in /app/components/dynamic-field/dynamic-field.directive.ts
Import ComponentFactoryResolver, ComponentRef, ViewContainerRef, FormGroup, FieldConfig and all acreage components.
Create a capricious componentMapper which map the acreage types (input, select, on, date, radioon, checkbox) to the agnate acreage component.
Change the selector aspect to [dynamicField] and actualize the @Input() bounden variables acreage and accumulation which acquire the ascribe from the ancestor basic and accredit it to the agnate acreage component.
Create a capricious componentRef of blazon any. It maintains the instance of dynamically created component.
Let’s inject “ComponentFactoryResolver” and “ViewContainerRef” casework in the constructor. The ComponentFactoryResolver will be acclimated to dness the basic at run time. This account contains resolveComponentFactory adjustment which can be acclimated to actualize a basic at run time. The ViewContainerRef to accretion admission to the appearance alembic of the aspect that will host the dynamically added component.
Implement ngOnInit lifecycle angle for creating the activating components.
Inside the ngOnInit function, we charge to accomplish afterward tasks.
Now we will actualize a activating anatomy which will accumulation all dynamically created acreage apparatus into a anatomy and it aggregates the ethics of anniversary acreage into object.
Create a agenda dynamic-form central app/components/ directory.
Create a basic alleged dynamic-form beneath app/components agenda and acceptation it to app.module.ts.
Let’s add all the necessory cipher in /app/components/dynamic-form/dynamic-form.directive.ts.
Import all all-important modules by abacus the beneath code.
Add the beneath cipher in the arrangement to actualize a acknowledging form. It uses the ng-container aspect to echo the dynamicField charge binding.
Create the afterward variables.
Use get to acknowledgment the anatomy value. It can be attainable from ancestor component.
Let’s inject FormBuilder in the constructor.
Add beneath cipher to actualize a control. It loops through the agreement fields and creates a ascendancy for anniversary acreage with validations and again add these dynamically created controls to the anatomy group.
Add beneath cipher to add validations to activating control.
Call createControl adjustment central the ngOnInit. It creates the ascendancy dynamically and allotment the FormGroup.
Add the beneath cipher to apparatus the abide functionality. if the anatomy is valid, the ancestor abide adjustment is accursed contrarily validation errors will be displayed.
Add beneath cipher to validate all anatomy fields.
Add dynamically created apparatus in entryComponents arrangement in app.module.ts. It lets Angular to abridge these components.
Almost done. Now we will actualize a simple Registration anatomy by appliance the dynamicform component.
Let’s add all the necessory cipher in /app/app.component.ts.
Import Validators, DynamicFormComponent, Basic and FieldConfig.
Use @ViewChild decorator to inject a advertence to dynamicform component.
Create a capricious RegConfig blazon of FieldConfig array. It describes the anatomy (fields and properties) of the form. The acreage backdrop will be capricious for altered blazon of fields.
Add the beneath cipher in /app/app.component.html.
Add the beneath css cipher in style.css.
Run the application.
Now the web server is started and the dynamically created Registration anatomy can be accessed on http://localhost:4200 as you see in the beneath screenshot.
You can acquisition all the antecedent cipher in the beneath repository.
The Hidden Agenda Of Html Dynamically Add Form Fields | Html Dynamically Add Form Fields – html dynamically add form fields
| Delightful to our blog, in this particular time I will teach you with regards to html dynamically add form fields