【发布时间】:2020-10-13 20:21:28
【问题描述】:
我已经被困在某事上几个小时了,我需要一些帮助。我对角度很陌生。 我正在使用响应式表单,展示我的代码并解释我想要完成的工作。
<mat-form-field>
<mat-select [(ngModel)]="filterCate"
placeholder="Cate" type="search" aria-label="Search"
formControlName="pro_cate_id"
name="search" (focus)="getControl($event)">
<mat-option *ngFor="let cate of categorias" [value]="cate.id">{{cate.name}}</mat-option>
</mat-select>
<button mat-button *ngIf="filterCate" matSuffix mat-icon-button aria-label="Clear"
(click)="$event.stopPropagation(); deleteField('pro_cate_id')">
<mat-icon>close</mat-icon>
</button>
</mat-form-field>
我有一个 ng-if,其中显示了一个按钮来重置字段,并且我只需要在字段中有值时显示它。我收到错误“类型 'xxxComponent' 上不存在属性 'filterCate'” 我知道通过双向我需要该值存在于组件中,如果我创建变量没有问题。但是我还有大约 20 个字段,而且我似乎没有为所有字段创建 20 个变量。在这种情况下我该怎么办,我怎么能在所有 mat-form-field 中没有 ng-model 的情况下重构这段代码?
【问题讨论】:
标签: angular typescript angular-reactive-forms formbuilder angular2-ngmodel