垫选择所需的验证不起作用 [英] mat-select required validation not working

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

问题描述

我有以下代码

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

即而不是这个

使用这个:

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆