Skip to content

Instantly share code, notes, and snippets.

View camilogiraldo's full-sized avatar

Camilo Giraldo camilogiraldo

View GitHub Profile
@camilogiraldo
camilogiraldo / Angular2-solution.md
Created August 15, 2019 18:48
Angular 2 solution
<app-dynamic-form></app-dynamic-form>
<form [formGroup]="form" (submit)="submitForm()">
<ng-template ngFor let-input [ngForOf]="formData">
<ng-container [ngSwitch]="input.controlType">
<ng-template [ngSwitchCase]="'text'">
<div class="form-group">
<label [for]="input.controlName"> {{input.controlName}}</label>
<input [formControlName]="input.controlName" [type]="input.valueType" [name]="input.controlName"
[required]="input.validators.required" [minlength]="input.validators.minlength"
<form [formGroup]="form" (submit)="submitForm()">
<ng-template ngFor let-input [ngForOf]="formData">
<ng-container [ngSwitch]="input.controlType">
<!-- handling text type inputs -->
<ng-template [ngSwitchCase]="'text'">
<div class="form-group">
<label [for]="input.controlName"> {{input.controlName}}</label>
<form [formGroup]="form" (submit)="submitForm()">
<ng-template ngFor let-input [ngForOf]="formData">
<ng-container [ngSwitch]="input.controlType">
<!-- here we will be dynamically creating our form fields-->
</ng-container>
</ng-template>
import { FormData } from './../../shared/interface/form-data';
import { Component, OnInit, Input } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'app-dynamic-form',
templateUrl: './dynamic-form.component.html',
styleUrls: ['./dynamic-form.component.scss']
})
export class DynamicFormComponent implements OnInit {
import { Component, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
@Component({
selector: 'app-dynamic-form',
templateUrl: './dynamic-form.component.html',
styleUrls: ['./dynamic-form.component.scss']
})
export class DynamicFormComponent implements OnInit {
form: FormGroup;
import { MockForm } from './shared/mock/mock-form';
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
data = MockForm;
<app-dynamic-form [formData]="data"></app-dynamic-form>
// Create this file under app/shared/mock/mock-data.ts
import { FormData } from './../interface/form-data';
export const MockForm: FormData[] = [
{
controlName: 'Username',
controlType: 'text',
valueType: 'text',
placeholder: 'Enter username',