I have an array in another array like that:
<div class="col-sm-6">
<div formArrayName="site_source_pages">
<div *ngFor="let site_source_page of siteForm.controls.site_source_pages.controls; let x=index">
<div [formGroupName]="x">
<div class="card card-accent-success">
<div class="card-header">
<strong>Yeni Kaynak Sayfa</strong>
</div>
<div class="card-block">
<div class="row">
<!--Site Page Source Form Group-->
<div class="form-group">
<div class="form-group col-sm-6">
<label for="page_url">Sayfa Url</label>
<!--<label *ngIf="!site_source_page.controls[x].controls.page_url.valid" class="text-danger">*</label>-->
<input type="text" class="form-control" id="page_url" placeholder="Badge-Manager" formControlName="page_url">
</div>
</div>
<div class="form-group col-sm-6">
<label for="postal-code">Tarama Aralığı</label>
<input type="text" class="form-control" id="postal-code" placeholder="Badge-Email">
</div>
</div>
<div class="row">
<div formArrayName="page_containers">
<div *ngFor="let page_container of site_source_page.controls.page_containers.controls; let i=index">
<div [formGroupName]="i">
<div class="col-sm-4">
<div class="form-group">
<label for="ccnumber">Container {{i + 1}} Type</label>
<label [hidden]="site_source_page.controls.page_containers.controls[i].controls.container_type.valid" class="text-danger">*</label>
<input type="text" class="form-control" id="container_type" placeholder="Container Type" formControlName="container_type">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label for="ccnumber">Container {{i + 1}} Selector Type</label>
<input type="text" class="form-control" id="selector_type" placeholder="Container Selector Type" formControlName="selector_type">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label for="ccnumber">Container {{i + 1}} Css Query</label>
<span class="glyphicon glyphicon-remove pull-right" *ngIf="site_source_page.controls.page_containers.controls.length > 1" (click)="removePageContainer(i)">x</span>
<input type="text" class="form-control" id="container_css_query" placeholder="Container Css Query" formControlName="selector_css_query">
</div>
</div>
<div class="col-sm-12">
<button type="button" class="btn btn-secondary btn-block" (click)="addPageContainer(x)">Container Ekle</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
I want to add item to second array via typescript. I try to reach second array (page_containers) but get this error:
Cannot read property 'controls' of undefined
My function is like that:
addPageContainer(x: number) {
// add container to the list
const controlSp = <FormArray>this.siteForm.controls['site_source_pages'];
const controlPc = <FormArray>controlSp.controls['page_containers'];
controlPc.push(this.initPageContainer());
}
Why can't I reach second array with this line?
const controlPc = <FormArray>controlSp.controls['page_containers'];
Any idea?
The problem is that
controlSp
is aFormArray
, so to accesspage_containers
, as any other commonarray
, you have to specify the index.You can do it using FormArray#at method:
or even better using
AbstractControl#get
method:Also, as you may have noticed I changed the type cast from
<variable>
toas variable
.Why? Because while the
as
syntax is fully compatible with JSX/TSX syntax, the other way isn't.Note that if you don't have any problem with this, you can use whatever you prefer.
That said, as an advice, you can simplify your HTML a lot.
Instead of (lines in order):
You can have (lines in order):
Also, just a minor note:
The following line is actually commented, but it's invalid:
It should be:
or even better: