Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save frankspin89/4a7aef0a9f99d2f018051852847b5e00 to your computer and use it in GitHub Desktop.
Save frankspin89/4a7aef0a9f99d2f018051852847b5e00 to your computer and use it in GitHub Desktop.
Dynamic form
import { Component, ViewContainerRef } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { Field } from '../../models/field.interface';
import { FieldConfig } from '../../models/field-config.interface';
@Component({
selector: 'app-form-input',
styleUrls: ['form-input.component.scss'],
template: `
<div class="form__field__wrapper">
<div
class="form__input__wrapper form__input__wrapper--{{config.size}}"
[ngClass]="{'form__input__wrapper--filled': 'inputValue.value.length > 0'}"
[formGroup]="group">
<label class="form__input__label">
<span class="form__input__label__content">{{ config.label }}</span>
</label>
<div *ngIf="inputValue.value.length > 0">Field is not empty</div>
<input
#inputValue
class="form__input form__input--text"
type="text"
[attr.placeholder]="config.placeholder"
[formControlName]="config.name"
>
</div>
<div class="form__field__error__wrapper" ngxErrors="{{config.name}}">
<div class="form__field__error" ngxError="required" when="touched">
{{ config.label }} is required
</div>
<div class="form__field__error" ngxError="minlength" [when]="['dirty', 'touched']">
Min length is {{config.minlength}}
</div>
</div>
</div>
`
})
export class FormInputComponent implements Field {
config: FieldConfig;
group: FormGroup;
constructor() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment