[英]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>
兩個問題:
<form>
元素內經過在黑暗中和博客網站上的許多絆腳石之后,我想通了。
對於 from angular 上的每個表單元素,期望有一個FromControl
object 來存儲表單元素的 state。 如果沒有FormControl
,angular 10 不會渲染表單元素,因為它沒有地方管理該元素的 state。 有兩種方法可以創建表單控件。
FormControl
對象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.