Skip to content

Commit e096a09

Browse files
committed
Moving logic into functions.
1 parent 524c2b3 commit e096a09

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

src/app/sub-forms/child-inner/child-inner.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<ng-container *ngFor="let item of fa.controls; let i = index">
55
<div [formGroup]="fa.controls[i]" [formGroupName]="i">
66
<label>Inner Item {{ i + 1 }} </label>
7-
<app-child2 [child2]="_childInner ? _childInner.fa[i].child2 : null"></app-child2>
7+
<app-child2 [child2]="child2(i)"></app-child2>
88
</div>
99
<button type="button" (click)="delete(i)">Delete</button>
1010
</ng-container>

src/app/sub-forms/child-inner/child-inner.component.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Component, Input, OnDestroy, OnInit } from '@angular/core';
22
import { AbstractControl, ControlContainer, FormArray, FormBuilder, FormGroup, FormGroupDirective } from '@angular/forms';
33
import { FormUtilsService } from 'src/app/core/form-utils.service';
44
import { ChildInner } from './child-inner.model';
5+
import { Child2 } from '../child2/child2.model';
56

67
@Component({
78
selector: 'app-child-inner',
@@ -44,6 +45,10 @@ export class ChildInnerComponent implements OnInit, OnDestroy {
4445
this.fa.removeAt(i);
4546
}
4647

48+
child2(i: number) {
49+
return this._childInner && this._childInner.fa[i] ? this._childInner.fa[i].child2 : ({ childShared1: {}, childShared2: {} } as Child2);
50+
}
51+
4752
faItemFactory(): AbstractControl {
4853
return this.fb.group({});
4954
}

src/app/sub-forms/child-outer/child-outer.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<ng-container *ngFor="let item of fa.controls; let i = index">
55
<div [formGroup]="fa.controls[i]" [formGroupName]="i">
66
<label>Outer Item {{ i + 1 }} </label>
7-
<app-child-inner [childInner]="_childOuter ? _childOuter.fa[i].childInner : null"></app-child-inner>
7+
<app-child-inner [childInner]="childInner(i)"></app-child-inner>
88
</div>
99
<button type="button" (click)="delete(i)">Delete</button>
1010
</ng-container>

src/app/sub-forms/child-outer/child-outer.component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Component, Input, OnDestroy, OnInit } from '@angular/core';
22
import { AbstractControl, ControlContainer, FormArray, FormBuilder, FormGroup, FormGroupDirective } from '@angular/forms';
33
import { FormUtilsService } from 'src/app/core/form-utils.service';
44
import { ChildOuter } from './child-outer.model';
5+
import { ChildInner } from '../child-inner/child-inner.model';
56

67
@Component({
78
selector: 'app-child-outer',
@@ -44,6 +45,14 @@ export class ChildOuterComponent implements OnInit, OnDestroy {
4445
this.fa.removeAt(i);
4546
}
4647

48+
childInner(i: number) {
49+
return this._childOuter && this._childOuter.fa[i] && this._childOuter.fa[i].childInner
50+
? this._childOuter.fa[i].childInner
51+
: ({
52+
fa: []
53+
} as ChildInner);
54+
}
55+
4756
faItemFactory(): AbstractControl {
4857
return this.fb.group({});
4958
}

0 commit comments

Comments
 (0)