Mat-Error在Mat-autocomplete中没有显示垫子和垫子场。

发布于 2025-01-19 16:38:18 字数 1146 浏览 2 评论 0 原文

我的目的是在用户未放置值时显示错误。我知道上面的示例没有太多的意义,但是如果程序必须显示,我也不会阅读错误。代码是:

<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”

my purpose is show an error when the user doesn't put a value. I know that above example doesn't have a lot of sense, but I don't read the error also if the programs must show. the code is this:

<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>

In my module I import:

 CommonModule,
    RicercaCircolariRoutingModule,
    ReactiveFormsModule,
    FormsModule,
    MatFormFieldModule,
    MatInputModule,
    MatSelectModule,
    MatAutocompleteModule,
    MatDialogModule,

The mat-error doesn't show also doesn't have condition. Anyone can help me?I don't read the error message "minlength 4"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲琵琶半遮面シ 2025-01-26 16:38:18

我建议不要为 ngmodel 与您在模板中使用的另一个属性相同的名称提供模板变量。我将首先更改该名称,然后仅在需要时显示错误。

如果您希望一个字段是强制性的,则只需要在其中添加必需的属性即可。

<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" required [(ngModel)]="flux" class="modInput" #fluxModel="ngModel">

        <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
            <ng-container *ngIf="existsFlux()">
                ...
            </ng-container>
    
        </mat-autocomplete>
        <mat-error *ngIf="fluxModel.control.hasError('required')">
          This field is required
        </mat-error>
    </mat-form-field>
</form>

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.

<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" required [(ngModel)]="flux" class="modInput" #fluxModel="ngModel">

        <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
            <ng-container *ngIf="existsFlux()">
                ...
            </ng-container>
    
        </mat-autocomplete>
        <mat-error *ngIf="fluxModel.control.hasError('required')">
          This field is required
        </mat-error>
    </mat-form-field>
</form>
在你怀里撒娇 2025-01-26 16:38:18

对我来说,解决方案是在自动完成的搜索控件上使用标记

<mat-form-field appearance="outline">
    <input type="text"
      [placeholder]="placeholder"
      aria-label="Number"
      matInput
      #autoInput
      [formControl]="searchCtrl"
      [matChipInputFor]="chipList"
      [matAutocomplete]="auto">
    <mat-autocomplete #auto="matAutocomplete" (optionSelected)="select($event)">

    ....

    @if (searchCtrl.hasError('test')) {
      <mat-error>This field is required</mat-error>
    }
</mat-form-field>

因此,在控制器中的功能中,我可以做类似的操作

this.searchCtrl.markAsTouched();
this.searchCtrl.setErrors({test:true })

,这将为我显示错误:

添加标记对它起作用至关重要。

For me the solution was to use markAsTouched on the search control in the autocomplete.

<mat-form-field appearance="outline">
    <input type="text"
      [placeholder]="placeholder"
      aria-label="Number"
      matInput
      #autoInput
      [formControl]="searchCtrl"
      [matChipInputFor]="chipList"
      [matAutocomplete]="auto">
    <mat-autocomplete #auto="matAutocomplete" (optionSelected)="select($event)">

    ....

    @if (searchCtrl.hasError('test')) {
      <mat-error>This field is required</mat-error>
    }
</mat-form-field>

So in a function in the controller, I can do something like

this.searchCtrl.markAsTouched();
this.searchCtrl.setErrors({test:true })

and it will show the error for me:

Adding markAsTouched was pivotal for it to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文