【问题标题】:How to hide selected option text in select box, Angular 4如何在选择框中隐藏选定的选项文本,Angular 4
【发布时间】:2019-04-05 07:04:51
【问题描述】:

我有这个种族下拉列表。 我想要的是当我选择明确的种族时,它不应该出现在选择框中,并且喜欢的框应该是空的。 我该怎么做?

  <select
        #ethnicity
        [(ngModel)]="patient.Ethnicity"
        [class.text-dimmed]="!patient.Ethnicity"
        name="ethnicity"
        id="ethnicity"
        class="form-control input-underline input-lg ml-0">
          <option [ngValue]="null" disabled> Select </option>
          <option [ngValue]="null">Clear ethnicity</option>
          <option  value="Alaska-Native">Alaska Native</option>
          <option value="American-Indian">American-Indian</option>
          <option value="Asian">Asian</option>
          <option value="African-Americans">Black or African American</option>
          <option value="Hispanic">Hispanic or Latino</option>
          <option value="Jewish-Ashkenazi">Jewish - Ashkenazi</option>
          <option value="Jewish-Other">Jewish - Other</option>
          <option value="Native-Hawaiians">Native Hawaiian or Other Pacific Islander </option>
          <option value="Other">Other </option>
          <option value="Unknown">Unknown </option>
          <option value="White-Caucasian">White or Caucasian</option>
      </select>

当我选择它时,它看起来像这样

但我想要的是,

这是我的demo 明确的种族应该显示在下拉列表中,但是当我选择它时,它不应该像我共享屏幕截图那样显示在选择框中

我应该怎么做才能解决这种情况? 谢谢。

【问题讨论】:

标签: javascript html css angular dropdown


【解决方案1】:

你可以像这样处理选择标签的change event

onChange(value) {
    if(value == 'Clear ethnicity'){
      this.Ethnicity = '';
}

<select
            #ethnicity
            [(ngModel)]="Ethnicity"
            [class.text-dimmed]="!Ethnicity"
            name="ethnicity" (change)="onChange($event.target.value)"
            id="ethnicity"
            class="form-control input-underline input-lg ml-0">

演示:https://stackblitz.com/edit/angular-clear-option-select

【讨论】:

  • 当我在选项标签中添加选择时,我还有一个小问题,它确实显示为在短选择中选择标签在此下拉列表中不起作用
【解决方案2】:

您所要做的就是删除值“清除种族”,如下所示:

<option [ngValue]="null" [hidden]="clear.selected"></option>

【讨论】:

  • 这就是我正在做的,但在选择框中仍显示明确的种族@pocketsofscene
  • 然后将 ngValue 设置为字符串
猜你喜欢
  • 2022-06-11
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
  • 2011-09-17
相关资源
最近更新 更多