Angular 2模型驱动表单中的默认无效选择选项?

时间:2016-06-27 20:44:31

标签: javascript angular angular2-forms

有没有办法在Angular 2模型驱动的表单中实现无效的默认选择选项?

我正在寻找类似的东西:

<select name="DateExpiryMonth" [(ngModel)]="model.DateExpiryMonth" ngControl="dateExpiryMonth" required>
    <option value="" selected="selected" disabled>Month...</option>
    <option *ngFor="let month of months" [value]="month">{{ month }}</option>
</select>

理想情况下,select上的required属性可确保Angular 2将第一个<option>视为无效,但我找不到任何表明这是可行的文档。

目前,Angular 2认为第一个“月......”选项有效。

1 个答案:

答案 0 :(得分:0)

我不认为这是一个理想的解决方案,但我发现如果我将模型中的字段设置为null或undefined(在您的情况下为model.dateExpiryMonth),则angular会认为表单无效