角4:当模型值为空时,默认情况下不选择具有“禁用"属性的选择选项 [英] Angular 4: Select option with 'disabled' attribute is not selected by default when model value is empty

查看:110
本文介绍了角4:当模型值为空时,默认情况下不选择具有“禁用"属性的选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当模型为空时,不会在Chrome/firefox中选择选择/禁用"选项.Safari可以很好地工作.

When the model is empty, the selected/disabled option is not selected in Chrome/firefox. This works perfectly fine is Safari.

<select [(ngModel)]="selectedProject" name="project" id="project">
   <option value="" [selected]=true [disabled]="true">Select project</option>
   <option [value]="project.id" *ngFor="let project of projects">{{project.title}}</option>
</select>

据报道这是AngularJS中的一个错误,但是[disabled]或disabled都不适合我.

This was reported as a bug in AngularJS, however, neither [disabled] or disabled works for me.

https://github.com/angular/angular.js/issues/7202

推荐答案

令人惊讶的是,这还没有得到答案.无论如何,这对我有用.

Surprised this wasn't answered still. Anyways this worked for me..

禁用它

<select [attr.disabled]=true class="custom-select" formControlName="title">
     <option [value]="project.id" *ngFor="let project of projects">{{project.title}} 
     </option>
</select>

并启用它

<select [attr.disabled]=null class="custom-select" formControlName="title">
     <option [value]="project.id" *ngFor="let project of projects">{{project.title}} 
     </option>
</select>

您也可以为其设置变量.就像魅力一样!

You can also set variable to it. Works like a charm!

这篇关于角4:当模型值为空时,默认情况下不选择具有“禁用"属性的选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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