When working with my "export class", I encountered an issue that led to a Promise error if I didn't include this line of code:
purchase = new Purchase();
right before the constructor. The error indicated that the property "name" was not found.
Although my HTML Template is set up to bind to the "purchase" class with:
<h2 text-center><input [(ngModel)]="purchase.name"></h2>
I expected the class to be provided by the Service Injector in the Constructor. However, explicitly declaring the structure of the "purchase"-class in the Detail Controller could pose challenges during unit testing.
Are there any suggestions on how to avoid the need for "purchase = new Purchase()"?
For reference, here is the complete code snippet:
import {Component, OnInit} from "@angular/core";
import {PurchaseService} from '../../services/purchase.service';
import {Purchase} from '../../services/purchase';
@Component({
templateUrl: 'build/pages/detail/detail.html',
providers: [PurchaseService] // teach injector how to make a PurchaseService
})
export class DetailPage implements OnInit{
name: String;
purchase = new Purchase();
constructor(
private _navController: NavController,
private _navParams: NavParams,
private purchaseService: PurchaseService) {
this._navController = _navController;
this.name = _navParams.get('name');
}
ngOnInit() {
this.getOnePurchase();
}
getOnePurchase() {
this.purchaseService.getOnePurchase(name)
.then(result => this.purchase = result
)
.then(result => console.log(result)
)
}
pushPage(name: string) {
this._navController.pop();
}
}