【问题标题】:Angular select placeholder not showing角度选择占位符未显示
【发布时间】:2021-12-02 23:52:24
【问题描述】:

我正在尝试添加一个默认显示占位符的选择,如下所示:

<select id="type" formControlName="type">
  <option hidden value="" [disabled]="true" [selected]="true">Placeholder</option>
  <option *ngFor="let item of ['test 1', 'test 2']" [value]="item">{{ item }}</option>
</select>

加载页面时,我没有在选择中看到占位符。但如果我这样做:

<select id="type" formControlName="type">
  <option hidden value="" [disabled]="true" [selected]="true">Placeholder</option>
  <option>test 1</option>
  <option>test 2</option>
</select>

我看到了。 *ngFor 不显示占位符有什么问题?

【问题讨论】:

标签: angular


【解决方案1】:

我在使用 select 占位符时遇到了类似的问题,并且每次都有不同的解决方案起作用。尝试只保留禁用并选中(删除隐藏),或者为您的 FormControlName 分配一个初始虚拟值,如 -1,然后将占位符选项值设置为 -1。

【讨论】:

    猜你喜欢
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 2015-01-29
    • 2016-04-03
    • 1970-01-01
    • 2019-11-04
    相关资源
    最近更新 更多