diff --git a/apps/parent-form/src/app/app.module.ts b/apps/parent-form/src/app/app.module.ts index c74dc50..4e5309e 100644 --- a/apps/parent-form/src/app/app.module.ts +++ b/apps/parent-form/src/app/app.module.ts @@ -3,12 +3,15 @@ import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { RouterModule } from '@angular/router'; +import { ReactiveFormsModule } from '@angular/forms'; +import { NameComponent } from './name/name.component'; @NgModule({ - declarations: [AppComponent], + declarations: [AppComponent, NameComponent], imports: [ BrowserModule, - RouterModule.forRoot([], { initialNavigation: 'enabled' }) + RouterModule.forRoot([], { initialNavigation: 'enabled' }), + ReactiveFormsModule, ], providers: [], bootstrap: [AppComponent] diff --git a/apps/parent-form/src/app/name/name.component.css b/apps/parent-form/src/app/name/name.component.css new file mode 100644 index 0000000..e69de29 diff --git a/apps/parent-form/src/app/name/name.component.html b/apps/parent-form/src/app/name/name.component.html new file mode 100644 index 0000000..35af8e4 --- /dev/null +++ b/apps/parent-form/src/app/name/name.component.html @@ -0,0 +1,26 @@ +
+
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+
diff --git a/apps/parent-form/src/app/name/name.component.spec.ts b/apps/parent-form/src/app/name/name.component.spec.ts new file mode 100644 index 0000000..ec53998 --- /dev/null +++ b/apps/parent-form/src/app/name/name.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { NameComponent } from './name.component'; + +describe('NameComponent', () => { + let component: NameComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ NameComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(NameComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/apps/parent-form/src/app/name/name.component.ts b/apps/parent-form/src/app/name/name.component.ts new file mode 100644 index 0000000..947d980 --- /dev/null +++ b/apps/parent-form/src/app/name/name.component.ts @@ -0,0 +1,34 @@ +import { FormGroup, FormBuilder, FormControl } from '@angular/forms'; +import { Name } from '@nested-forms/contact-model'; +import { Component, OnInit, Input } from '@angular/core'; + +@Component({ + selector: 'nested-forms-name', + templateUrl: './name.component.html', + styleUrls: ['./name.component.css'] +}) +export class NameComponent implements OnInit { + + @Input() name: Name; + @Input() parent: FormGroup; + + group: FormGroup; + + constructor(private fb: FormBuilder) { + } + + ngOnInit() { + this.group = this.fb.group({ + firstName: new FormControl(this.name ? this.name.firstName : ''), + lastName: new FormControl(this.name ? this.name.lastName : ''), + middleName: new FormControl(this.name ? this.name.middleName : ''), + prefix: new FormControl(this.name ? this.name.prefix : ''), + suffix: new FormControl(this.name ? this.name.suffix : ''), + }); + + if (this.parent) { + this.parent.addControl('name', this.group); + } + } + +}