Skip to content

Instantly share code, notes, and snippets.

@smitch88
Created July 7, 2016 04:23
Show Gist options
  • Save smitch88/202571613bf5651f3ea24a186f2fd1d8 to your computer and use it in GitHub Desktop.
Save smitch88/202571613bf5651f3ea24a186f2fd1d8 to your computer and use it in GitHub Desktop.
import { Component, Input, OnInit, EventEmitter, Output } from '@angular/core';
import { FORM_DIRECTIVES } from '@angular/common';
import { Store } from '@ngrx/store';
import { FormControl,
FormBuilder,
FormGroup,
REACTIVE_FORM_DIRECTIVES,
Validators } from '@angular/forms';
import { Observable } from 'rxjs/Observable';
import { Person } from '../../models/';
@Component({
selector: 'person-form',
directives: [ REACTIVE_FORM_DIRECTIVES ],
templateUrl: './people.form.template.html'
})
export class PersonForm {
@Input() person: Person;
@Output() onSave: EventEmitter<any> = new EventEmitter();
private personForm: FormGroup;
constructor( private formBuilder: FormBuilder ){
this.personForm = this.formBuilder.group({
urn: [ '', Validators.required ],
username: [ '', Validators.required ],
roles: [ [], Validators.required ],
authorities: [ [], Validators.required ],
tenantUrn: [ '', Validators.required ],
emailAddress: [ '', Validators.required ],
active: [ true, Validators.required ],
givenName: [ '', Validators.required ],
surname: [ '', Validators.required ]
});
}
onSubmit( e ): void {
e.preventDefault();
this.onSave.emit( this.personForm.value );
}
ngOnChanges( changes: any ): void {
let personChanged: Person = changes.person.currentValue;
if( personChanged ){
setTimeout(function(){
Object.keys( personChanged ).map(function( key, index ){
(<FormControl>this.personForm.controls[ key ]).updateValue( personChanged[ key ] )
}.bind(this));
}.bind(this), 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment