簡體   English   中英

調試angular組件不顯示在屏幕上的技巧是什么?

[英]What are the techniques for debugging angular components that are not displaying on the screen?

我是 angular 和 angular 材料的新手,我的觀點非常簡單

<div class="mat-headline">Product Inventory Search</div>

<form (ngSubmit)="search()">
  <mat-form-field>
    <mat-label>Product</mat-label>
    <input matInput type="text" [formControl]="product"  />
  </mat-form-field>
</form>

當它顯示時,我看不到任何輸入字段在此處輸入圖像描述

如果我刪除mat-form-field周圍的form元素,如下所示

<div class="mat-headline">Product Inventory Search</div>

  <mat-form-field>
    <mat-label>Product</mat-label>
    <input matInput type="text" [formControl]="product"  />
  </mat-form-field>

它有效,我可以看到如下所示的產品字段在此處輸入圖像描述

兩個問題:

  • 如何將 angular 材料輸入字段放置在<form>元素內
  • 當屏幕上未顯示某些內容時,angular 提供什么工具來調試原因。 就我而言,我沒有看到任何錯誤消息,只是沒有顯示該組件。

經過在黑暗中和博客網站上的許多絆腳石之后,我想通了。

對於 from angular 上的每個表單元素,期望有一個FromControl object 來存儲表單元素的 state。 如果沒有FormControl ,angular 10 不會渲染表單元素,因為它沒有地方管理該元素的 state。 有兩種方法可以創建表單控件。

  • 模板驅動,其中 angular 創建FormControl對象
  • 開發人員必須在組件class中創建FormControl的反應式

在反應式表單的情況下,可以有多個表單元素,因此必須由開發人員構建FormGroup object 並與表單相關聯。 下面的代碼有效。

<form [formGroup]="searchForm"  (ngSubmit)="search()">
  <mat-form-field>
    <mat-label>Product</mat-label>
    <input matInput type="text" formControlName="product"  />
  </mat-form-field>
  <button mat-button color="primary">search</button>
</form>

我的錯誤版本的主要變化是[formGroup]="searchForm searchForm 和formControlName="product"而不是[formControl]="product"

我對事情的誤解現在已經消除,但我仍在尋找將來更有效地調試此類問題的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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