assets/angular/shared/form/dmp-field.component.ts
Base component for a DMP field.
Author: Shilo Banihit
selector | dmp-field |
template |
|
Properties |
Methods |
Inputs |
Accessors |
constructor(componentFactoryResolver: ComponentFactoryResolver, app: ApplicationRef)
|
||||||||||||
For DI'ing...
Parameters :
|
field
|
The model for this field.
Type: |
fieldMap
|
Field map
Type: |
form
|
Form group
Type: |
value
|
The value of this field.
Type: |
ngOnChanges |
ngOnChanges()
|
Change handler, instantiates the field component.
Returns :
void
|
fieldAnchor |
fieldAnchor:
|
Type : ViewContainerRef
|
Decorators : ViewChild
|
The DOM node for this field. |
isValid |
getisValid()
|
import { Component, Input, Inject, ViewChild, ViewContainerRef, ComponentFactoryResolver, ComponentRef, ApplicationRef } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FieldBase } from './field-base';
import { SimpleComponent } from './field-simple.component';
/**
* Base component for a DMP field.
*
* Author: <a href='https://github.com/shilob' target='_blank'>Shilo Banihit</a>
*
*/
@Component({
selector: 'dmp-field',
template: '<div #field></div>'
})
export class DmpFieldComponent {
/**
* The model for this field.
*/
@Input() field: FieldBase<any>;
/**
* Form group
*/
@Input() form: FormGroup;
/**
* The value of this field.
*/
@Input() value: any;
/**
* Field map
*/
@Input() fieldMap: any;
/**
* The DOM node for this field.
*/
@ViewChild('field', {read: ViewContainerRef}) fieldAnchor: ViewContainerRef;
/**
* For DI'ing...
*/
constructor(@Inject(ComponentFactoryResolver) private componentFactoryResolver: ComponentFactoryResolver, protected app: ApplicationRef){
}
/**
* If the form is valid.
*/
get isValid() {
if (this.form && this.form.controls) {
return this.form.controls[this.field.name].valid;
}
return false;
}
/**
* Change handler, instantiates the field component.
*/
ngOnChanges() {
if (!this.field || !this.componentFactoryResolver) {
return;
}
this.fieldAnchor.clear();
let compFactory = this.componentFactoryResolver.resolveComponentFactory(this.field.compClass);
let fieldCompRef:ComponentRef<SimpleComponent> = <ComponentRef<SimpleComponent>> this.fieldAnchor.createComponent(compFactory, undefined, this.app['_injector']);
fieldCompRef.instance.injector = this.app['_injector'];
fieldCompRef.instance.field = this.field;
fieldCompRef.instance.form = this.form;
fieldCompRef.instance.fieldMap = this.fieldMap;
this.fieldMap[this.field.name].instance = fieldCompRef.instance;
}
}