File

assets/angular/shared/form/dmp-field.component.ts

Description

Base component for a DMP field.

Author: Shilo Banihit

Metadata

selector dmp-field
template
<div #field></div>

Index

Properties
Methods
Inputs
Accessors

Constructor

constructor(componentFactoryResolver: ComponentFactoryResolver, app: ApplicationRef)

For DI'ing...

Parameters :
Name Type Optional Description
componentFactoryResolver ComponentFactoryResolver
app ApplicationRef

Inputs

field

The model for this field.

Type: FieldBase<any>

fieldMap

Field map

Type: any

form

Form group

Type: FormGroup

value

The value of this field.

Type: any

Methods

ngOnChanges
ngOnChanges()

Change handler, instantiates the field component.

Returns : void

Properties

fieldAnchor
fieldAnchor: ViewContainerRef
Type : ViewContainerRef
Decorators : ViewChild

The DOM node for this field.

Accessors

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;
  }
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""