本文介绍了垫选择所需的验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

<form #createForm="ngForm">
 <mat-form-field>
   <mat-select placeholder="Favorite food"
      matInput
     [ngModel]
     food="food"
     #food="ngModel" required>
    <mat-option *ngFor="let food of foods" [value]="food.value">
      {{ food.viewValue }}
    </mat-option>
   </mat-select>
</mat-form-field>
</form>
<button [disabled]="!createForm.valid">submit</button>

由于我希望选择"是必填字段,因此在呈现表单时应禁用提交"按钮.但是,显示表单时将启用提交"按钮.有什么问题吗?

Since I want the "selection" is a required field, the "submit" button should be disabled when the form is rendered. However, the "submit" button is enabled when the form is displayed. What is the problem?

推荐答案

当我(a)使用 name 属性并且(b)使用双向 ngModel时,这对我有用绑定语法.

This works for me when I (a) use a name attribute and (b) use the two-way ngModel binding syntax.

即代替这个

< mat-select占位符=收藏的食物" matInput [ngModel] food ="food"#food ="ngModel"必填>

使用此:

< mat-select name ="food"占位符=最喜欢的食物" [(ngModel)] =" food必填>

这篇关于垫选择所需的验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 21:13