У меня есть интерфейс следующим образом:
import {IRecipients} from "./irecipients"; export interface IDataProcessingSpecification {
user:string;
businessID:string;
recipient:IRecipients[];
}
Получатель выглядит следующим образом:
export interface IRecipients {
recipientName:string,
recipientLocation:string[]
}
и здесь я инициализировал переменную типа IDataProcessingSpecification:
generatedDataProcessing:IDataProcessingSpecification=
{
user:'',
businessID:'',
recipient:[{recipientName:'',recipientLocation:[]}],
}
Получатели один или несколько. чтобы реализовать то, что я следовал этому
учебнику , то есть разрешить одному или нескольким получателям. поэтому получатель содержит два элемента управления: текстовое поле для имени и флажок для местоположения.
когда пользователь выберет одно место из флажка, будет запущен следующий код:
onRecipientLocation(event,location,i) {
console.log("here " + location+ " " + i )
if (event.target.checked) {
this.generatedDataProcessing.recipient[i].recipientLocation.push(location)
console.log("location "+ i+ this.generatedDataProcessing.recipient[i].recipientLocation)
}
}
}
так, для первого получателя функция флажка работает нормально, но когда добавляешь другой элемент управления, нажимая кнопку добавления, я получаю эту ошибку «Не удается прочитать свойство'ientientLocation 'неопределенного» при выборе из флажка
РЕДАКТИРОВАТЬ вот HTML-код:
<div *ngFor="let address of dataProcessingForm.controls.linktodrive.controls; let i=index">
<div>
<span>Name the recipient: </span>
<span *ngIf="dataProcessingForm.controls.linktodrive.controls.length > 1">
<a (click)="removeLink(i)"> Remove </a></span>
</div>
<!-- Angular assigns array index as group name by default 0, 1, 2, ... -->
<div [formGroupName]="i">
<input type="text" placeholder="*Enter Recipient" formControlName="recipientName">
<div>
<label for="recipientLocation"><h3> Recipient Location:</h3> </label>
<div *ngFor="let obj of recipientLocation">
<input type="checkbox"
name="recipientLocation"
formControlName="recipientLocation"
value="{{obj}}"
(change)="onRecipientLocation($event,obj,i)"
>
{{obj}}
</div>
</div>
</div>
</div>
<div><a (click)="addLink()"> Add </a></div>