![](/img/trans.png)
[英]angular2: strange result when using ngFor with a template that's supposed to represent a table row
[英]Angular2: Very strange/sudden issue when using ngSubmit to pass a String to a function
我正忙於Angular2 Web應用程序,並且遇到了一個非常奇怪的問題……我有一個模型驅動的表單,該表單使用ngSubmit
將條形碼值傳遞給findProduct()
函數。 大約一個小時前,一切正常,但是現在,當我單擊“掃描”按鈕時,頁面將重新加載,並且在google dev工具控制台中沒有任何輸出。 我嘗試在findProduct()
函數中使用console.log條形碼,但在控制台中仍然沒有輸出,只要單擊按鈕,頁面就會重新加載。 知道我在做什么錯/如何調試此類問題?
我的代碼:
html模板:
<form role="form" [formGroup]="productForm" (ngSubmit)="findProduct(productForm.value.barcode)">
<input class="form-control" type="number" placeholder="Enter Barcode" [formControl]="productForm.controls['barcode']">
<small [hidden]="productForm.controls['barcode'].valid || (productForm.controls['barcode'].pristine && !productForm.submitted)">barcode required</small>
<button class="btn btn-submit pull-right" [disabled]="!productForm.valid">Scan</button>
</form>
控制器:
findProduct(barcode: string) {
console.log(barcode);
this.product.GoodsReceivedID = this.GRNIDKey;
this.restService.findCaptureItem(this.GRNIDKey, barcode)
.subscribe(
(res) => {
console.log(res);
this.product = res;
},
(res) => {
console.log(res);
});
}
服務:
findCaptureItem(GRNIDKey: string, barcode: string) : Observable<any> {
return this.http.get(this.API_URL + 'PurchaseOrder/FindPurchaseOrderItem/' + GRNIDKey + '/' + barcode)
.map((res: Response) => res.json())
.catch((error: any) => Observable.throw(error.json().error || 'server error'));
}
事實證明,該問題發生在網上:
this.product.GoodsReceivedID = this.GRNIDKey;
問題是this.product已經聲明但尚未初始化...我覺得很奇怪,Google開發人員控制台或angular-cli(Windows命令提示符)中都沒有警告/錯誤...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.