Skip to content

Instantly share code, notes, and snippets.

@tobias74
tobias74 / basic-input.component.ts
Created October 5, 2018 13:49
Basic Input Component
import { Component, HostBinding, Input, ViewChild } from '@angular/core';
import { AbstractControl, ControlContainer, FormGroup } from '@angular/forms';
@Component({
template: `
<div style="margin:20px;">
<div style="display:inline-block; width:100px;">
{{label}}
</div>
<div style="display:inline-block;">
@tobias74
tobias74 / app.component.ts
Created October 5, 2018 13:45
Example Application using dynamic form component
import { Component, ViewChild } from '@angular/core';
import { BasicInputComponent } from "./shared";
import { DummyComponentOne, DummyComponentTwo } from "./dummy.components";
import { DynamicFormComponent } from "./shared";
@Component({
selector: 'my-app',
template: `
<h2>Dynamic Form</h2>
@tobias74
tobias74 / dynamic-loading.ts
Last active October 5, 2018 13:40
Load dynamic component
export class DynamicFormComponent implements AfterViewChecked {
@ViewChild(ComponentPlaceholderDirective) componentPlaceholder: ComponentPlaceholderDirective;
...
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(DummyComponent);
componentPlaceholder.viewContainerRef.clear();
const componentRef = componentPlaceholder.viewContainerRef.createComponent(componentFactory);
@tobias74
tobias74 / placeholder.directive.ts
Created October 5, 2018 13:34
Placeholder directive for dynamic component loading
import { Directive, Input, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[componentPlaceholder]',
})
export class ComponentPlaceholderDirective {
@Input()
placeholderName: string;
constructor(public viewContainerRef: ViewContainerRef) {}
import {AfterViewChecked, ChangeDetectorRef, Component,
ComponentFactoryResolver, EventEmitter, Input, QueryList,
ViewChildren, ViewEncapsulation,
} from '@angular/core';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { ComponentPlaceholderDirective } from '../directives';
@Component({
selector: 'dynamic-form',
template: `
@tobias74
tobias74 / app.component-part1.ts
Created October 5, 2018 10:09
shuffleComponents in app.component.ts
export class AppComponent {
@ViewChild(DynamicFormComponent) dynamicFormComponent: DynamicFormComponent;
public shuffleComponents(){
this.shuffleCount++;
this.formDefinition.filter( element => !element.isFormInput).forEach( (element) => {
element.componentInputs.myContent = "Altered Content " + this.shuffleCount;
this.dynamicFormComponent.applyComponentInputs(element.elementName, element.componentInputs);
});
this.formDefinition = this.shuffle(this.formDefinition);
@tobias74
tobias74 / dynamic-form-part4.component.ts
Created October 5, 2018 10:06
applyComponentInputs in dynamic-form.component.ts
export class DynamicFormComponent implements AfterViewChecked, DoCheck, OnInit {
...
public applyComponentInputs(elementName, componentInputs){
const componentInstance = this.getComponentInstanceByName(elementName);
if (componentInstance) {
Object.assign(componentInstance, componentInputs);
}
}
}
@tobias74
tobias74 / form-definition.ts
Last active October 5, 2018 09:26
example form-definition
this.formDefinition = [
{
isFormInput: true,
isVisible: true,
elementName: 'form_id_001',
defaultValue: '',
validators: [],
componentClass: BasicInputComponent,
componentInputs: <BasicInputComponent>{
formControlName: 'form_id_001',
@tobias74
tobias74 / app.component-part1.html
Last active October 5, 2018 09:18
dynamic-form-component usage example in html-template
<h2>Dynamic Form</h2>
<dynamic-form [formDefinition]="formDefinition"
(formDataChanged)="updateFormData($event)"
(createFormGroup)="setFormGroup($event)"
>
</dynamic-form>
@tobias74
tobias74 / dynamic-form-part3.component.ts
Created October 5, 2018 09:03
ngAfterViewChecked in dynamic-form.component.ts
export class DynamicFormComponent implements AfterViewChecked, DoCheck, OnInit {
...
ngAfterViewChecked() {
this.updateComponentPlaceholders();
this.updateInputStates();
this._changeDetectionRef.detectChanges();
}
}