nested-forms/apps/parent-form/src/app/address/address.component.ts

34 lines
916 B
TypeScript

import { FormArray, FormBuilder, FormGroup, FormControl } from '@angular/forms';
import { Address } from '@nested-forms/contact';
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'nested-forms-address',
templateUrl: './address.component.html',
styleUrls: ['./address.component.css']
})
export class AddressComponent implements OnInit {
@Input() address: Address;
@Input() parent: FormArray;
group: FormGroup;
constructor(private fb: FormBuilder) { }
ngOnInit() {
this.group = this.fb.group({
line1: [this.address ? this.address.line_1 : ''],
line2: [this.address ? this.address.line_2 : ''],
city: [this.address ? this.address.city : ''],
state: [this.address ? this.address.state : ''],
postalCode : [this.address ? this.address.postalCode : ''],
});
if (this.parent) {
this.parent.push(this.group);
}
}
}