簡體   English   中英

Angular2:使用ngSubmit將字符串傳遞給函數時,非常奇怪/突然的問題

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM