【问题标题】:Default value for <select> using ng-input<select> 使用 ng-input 的默认值
【发布时间】:2017-03-17 16:55:51
【问题描述】:

在我的情况下,有什么方法可以在不使用控制器的情况下为 select 设置默认值?

我尝试了很多方法。这是最后一个...

<select [(ngModel)]="model.carColour" [ngModelOptions]="{standalone: true}" class="form-control input-sm" ng-init="model.carColour='select'">
     <option value="select">Select color</option> 
     <option></option>   
     <option *ngFor="let item of carscolours" [value]="item.value"> {{item.text}}</option>  
 </select>

我需要将“选择颜色”设置为默认值,下拉列表中将是空字符串和库中的项目。

【问题讨论】:

  • 问题仍然存在

标签: angular select default ng-init ngmodel


【解决方案1】:

找到答案。也许有人会需要它...

<select [(ngModel)]="model.carColour"  #States="ngModel" name="carColours" class="form-control input-sm" >
       <option [ngValue]="model.carColour" hidden>Please, select...</option>
       <option></option>
       <option *ngFor="let item of carscolours" [ngValue]="item.value" >{{item.text}}</option>
    </select>

【讨论】:

  • 谢谢! - 我一直在寻找这个!
【解决方案2】:
<option selected="selected">Select Color</option>

【讨论】:

  • 我不明白这怎么行。当您检查该选项时,它是否没有这些属性?它的默认值是什么?
  • 如果
  • 幸运的是,您的问题已经被问过无数次了。请参阅:stackoverflow.com/questions/17329495/…stackoverflow.com/questions/18194255/…stackoverflow.com/questions/22007196/…。下次尝试搜索!
  • 我搜索了一个小时。并看到了所有这些答案。他们没有解决我的问题。
  • 我怀疑您当时没有正确实施解决方案,但是您没有显示任何您实际尝试过的 JS 代码,所以我们无法告诉您您做错了什么。不幸的是,我不熟悉 Angular,但它看起来是您在配置中设置的东西,但很多答案显示了许多解决它的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
  • 2018-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多