I am currently working on an Angular 8 application that involves a parent-child relationship.
The parent component, DossierCorrespondenceComponent, contains a function that needs to be triggered from within the child component.
This is the function in the parent component:
@Input() myGotoItem: Function;
gotoItem(index, type: string) {
this.showingSingle = true;
switch (type) {
case 'correspondence': {
this.single = this.correspondenceEntries[index];
break;
}
case 'attachments': {
this.single = this.attachmentEntries[index];
break;
}
default: {
break;
}
}
this.showingSingle = true;
}
When trying to call this function in the child component, DossierCorrespondenceListComponent, I used the following code:
<tr class="dossier-correspondencerow" *ngFor="let entry of correspondenceEntries; let i = index" (myGotoItem)="gotoItem(i, entry.type)">
However, nothing happens when this interaction takes place.
So, what changes do I need to make to resolve this issue?
In addition, another component, DossierCorrespondenceAttachmentsComponent, also needs to utilize the same function:
<tr class="dossier-correspondencerow" *ngFor="let entry of attachmentEntries; let i = index" (click)="gotoItem(i, entry.type)">
If I move the function gotoItem(index, type: string)
to both child components, it will result in duplicate code.
Currently, the parent component is structured as follows:
<ng-container *ngIf="correspondenceEntries">
<app-dossier-correspondence-list [correspondenceEntries]="correspondenceEntries" (onClick)="gotoItem(i, entry.type)"> </app-dossier-correspondence-list>
</ng-container>
And the TypeScript code in the parent component:
gotoItem(index, type: string) {
this.showingSingle = true;
switch (type) {
case 'correspondence': {
this.single = this.correspondenceEntries[index];
break;
}
case 'attachments': {
this.single = this.attachmentEntries[index];
break;
}
default: {
break;
}
}
this.showingSingle = true;
}
For the child component:
<tbody class="dossier-tablebody">
<tr class="dossier-correspondencerow" *ngFor="let entry of correspondenceEntries; let i = index" (click)="gotoItem(i, entry.type)">
<td>{{ entry.date | date:"dd-MM-y" }}</td>
<td>{{ entry.name }}</td>
</tr>
</tbody>
And the TypeScript code in the child component:
export class DossierCorrespondenceListComponent implements OnInit {
@Input()
correspondenceEntries: DossierEntry[];
@Output()
onClick = new EventEmitter();
@Input() showingSingle;
constructor() { }
ngOnInit() {
}
ngOnChanges(changes) { console.log(changes)}
click() {
this.onClick.emit();
}
}
However, after making these changes, I encountered the following error:
dossier-correspondence-list.component.html:13 ERROR TypeError: _co.gotoItem is not a function
at Object.handleEvent (dossier-correspondence-list.component.html:13)
at handleEvent (core.js:29733)
at callWithDebugContext (core.js:30803)
at Object.debugHandleEvent [as handleEvent] (core.js:30530)
at dispatchEvent (core.js:20520)
at core.js:28942
at HTMLTableRowElement.<anonymous> (platform-browser.js:1009)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:26760)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
To address this issue, I made the following change:
click() {
this.onClick.emit(true);
}
dossier-correspondence-item.component.ts:51 ERROR TypeError: _co.gotoItem is not a function
at Object.handleEvent (dossier-correspondence-item.component.ts:51)
at handleEvent (core.js:29733)
at callWithDebugContext (core.js:30803)
at Object.debugHandleEvent [as handleEvent] (core.js:30530)
at dispatchEvent (core.js:20520)
at core.js:28942
at HTMLTableRowElement.<anonymous> (platform-browser.js:1009)
at