简体   繁体   中英

Default invalid select option in Angular 2 model-driven forms?

Is there any way to achieve an invalid default select option in an Angular 2 model driven form?

I'm looking for something like this:

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

Ideally, the required attribute on the select would ensure that the first <option> would be considered invalid by Angular 2, yet I can't find any documentation that suggests this is possible.

As it stands, the first "Month..." option is considered valid by Angular 2.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM