My experience with Ionic 2 involves a component that consists of two smaller components, and data sharing is done through emitters. However, upon running the program, I encounter this particular error:
Runtime Error Uncaught (in promise): TypeError: Cannot read property 'BillNo' of undefined TypeError: Cannot read property 'BillNo' of undefined at Object.eval [as updateDirectives]
Let me share my code for better understanding:
bill-settlement.html
...
<page-bill-list (BillSelected)="onBillSelected($event)"></page-bill-list>
...
<page-bill-details [billItem]="billItem"></page-bill-details>
...
bill-settlement.ts
@Component({
selector: 'page-bill-settlement',
templateUrl: 'bill-settlement.html',
})
export class BillSettlement {
...
billItem: BillDetail
...
onBillSelected(billData: BillDetail) {
this.billItem = billData
}
}
bill-list.html
<ion-buttons>
<button ion-button *ngFor="let item of billItems" (click)="getBillDetails(item)">
{{item.BillNo}}
</button>
</ion-buttons>
bill-list.ts
@Component({
selector: 'page-bill-list',
templateUrl: 'bill-list.html',
})
export class BillList {
billItems: BillDetail[] = []
billItem = new BillDetail()
@Output() BillSelected = new EventEmitter<BillDetail>()
constructor(public navCtrl: NavController,
public navParams: NavParams,
public billSrv: BillerService,
public authSrv: AuthService,
public genSrv: GenericService) {
this.billSrv.getBills()
.subscribe(data => {
this.billItems = data
})
}
getBillDetails(item: BillDetail) {
this.BillSelected.emit(this.billItem)
}
}
bill-details.ts
@Component({
selector: 'page-bill-details',
templateUrl: 'bill-details.html',
})
export class BillDetails {
...
@Input() billItem: BillDetail
...
}
bill-details.html
...
<ion-input text-right type="text" [value]="billItem.BillNo" readonly></ion-input> //billItem model has BillNo property
...
The main issue lies in the fact that initially, billItem.BillNo
in bill-details.ts
is undefined. It only gets defined when clicking the bill number button in bill-list.html
. How can I set an initial value for billItem and then replace it upon button click?