1
在我的「出口類:」如果我不這樣做:Angular2:需要實例類,以防止無極/物業未找到錯誤
purchase = new Purchase();
我的構造函數之前,然後我得到一個錯誤的承諾,即找不到「名稱」。
我的HTML模板確實結合類:
<h2 text-center><input [(ngModel)]="purchase.name"></h2>
但我希望由服務噴油器在構造函數中提供了這個類。現在我必須告訴我的詳細控制器關於「購買」類的結構,我可能在單元測試時遇到問題。
任何想法如何避免「purchase = new Purchase()」?
下面是完整的代碼:
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();
}
}