Mat-Error在Mat-autocomplete中没有显示垫子和垫子场。
我的目的是在用户未放置值时显示错误。我知道上面的示例没有太多的意义,但是如果程序必须显示,我也不会阅读错误。代码是:
<form (ngSubmit)="ricerca()" #formCampiRicerca="ngForm">
<mat-form-field appearance="legacy" class="col-sm-12 col-lg-6 w-100 p-3">
<mat-label>{{ "APPLICATION......" | }}</mat-label>
<input type="text" [(ngModel)]="flux" class="modInput" #flux="ngModel">
<mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
<ng-container *ngIf="existsFlux()">
...
</ng-container>
</mat-autocomplete>
// here the error doesn't show also it doesn't have a filed
<mat-error>minlength 4</mat-error>
</mat-form-field>
</form>
在我的模块中,我导入:
CommonModule,
RicercaCircolariRoutingModule,
ReactiveFormsModule,
FormsModule,
MatFormFieldModule,
MatInputModule,
MatSelectModule,
MatAutocompleteModule,
MatDialogModule,
Mat-Error显示也没有条件。任何人都可以帮助我吗?我没有阅读错误消息“最小长度4”
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议不要为
ngmodel
与您在模板中使用的另一个属性相同的名称提供模板变量。我将首先更改该名称,然后仅在需要时显示错误。如果您希望一个字段是强制性的,则只需要在其中添加
必需的
属性即可。I recommend not giving your template variable for the
NgModel
the same name as another property that you use in the template. I would start by changing that name first, then show the error only when needed.If you want a field to be mandatory, you just need to add the
required
attribute to it.对我来说,解决方案是在自动完成的搜索控件上使用
标记
。因此,在控制器中的功能中,我可以做类似的操作
,这将为我显示错误:

添加
标记
对它起作用至关重要。For me the solution was to use
markAsTouched
on the search control in the autocomplete.So in a function in the controller, I can do something like
and it will show the error for me:

Adding
markAsTouched
was pivotal for it to work.