Here is an Angular component written in Typescript that I am currently working on:
export class ItemsComponent implements OnInit {
@Input()
invoice: Document;
@Input()
declaration: Document;
private invoiceName: string;
private declarationName: string;
constructor(private dataService: DataService) {
}
ngOnInit(): void {
this.getDocumentNames();
}
getDocumentNames() {
this.getSingleDocumentName(this.invoice, (name) => this.invoiceName = name);
this.getSingleDocumentName(this.declaration, (name) => this.declarationName = name);
}
getSingleDocumentName(document: Document, callback: Function) {
this.dataService.getDocumentName(document.id)
.subscribe((name) => callback(name));
}
Although the current setup works fine, it feels repetitive and inefficient to me. I would like to refactor it so that I can have just one method like getDocumentName
, which I can call once inside the onInit
to retrieve the names of both my invoice
and declaration
separately. Do you have any suggestions on how to achieve this?