如何在Angular 8中的Select标签上设置占位符?

时间:2019-12-23 05:58:55

标签: angular select angular8

我正在创建一个下拉列表以过滤数据,所以我创建了一个下拉列表

<form  #filter="ngForm" (ngSubmit)="filterta(filter)" style="display: flex;">
       <select  ngModel #month="ngModel" name="month" required  >
                <option [ngValue]="null" [disabled]="true" >All</option>
                <option value="1">January</option>
       </select>
</form>

当我从select标签中删除ngModel #month="ngModel"这些属性时,它会显示占位符选项All;当我添加这些属性时,它会在占位符中显示Blank。

6 个答案:

答案 0 :(得分:3)

这在Angular-10中对我有用。

public class Sentinel {

    public static void main(String args []) {
        Scanner scan = new Scanner(System.in);
        String temp = scan.next();
        int count = 0;
        double total = 0;
        while (isNumeric(temp))
        {
            total += Double.parseDouble(temp);
            count++;
            temp = scan.next();
        }
        System.out.println(BigDecimal.valueOf(total/count).setScale(3, RoundingMode.HALF_UP).doubleValue());
    }

    public static boolean isNumeric(String str) {
        return str.matches("-?\\d+(\\.\\d+)?");  //match a number with optional '-' and decimal.
    }
}

答案 1 :(得分:1)

month = null中设置component.ts的初始值,并在[(ngModel)]="month"的{​​{1}}标签中添加select

component.ts

component.html;

component.html

month = null

答案 2 :(得分:1)

尝试这样:

  • ngModel #month="ngModel"修改为[(ngModel)]="selectedMonth" #month
  • 在.ts文件中,添加selectedMonth = null;

.ts

selectedMonth = null;

.html

<form  #filter="ngForm" (ngSubmit)="filterta(filter)" style="display: flex;">
       <select  [(ngModel)]="selectedMonth" #month name="month" required  >
                <option [ngValue]="null" [disabled]="true" >All</option>
                <option value="1">January</option>
       </select>
</form>

Working Demo

答案 3 :(得分:1)

您需要稍微更改代码

html文件

[(ngModel)]="month" // This will set month value for select tag

在ts文件中

将月份值设置为默认值

month = null;

工作代码

https://stackblitz.com/edit/angular-plwvgg

答案 4 :(得分:0)

<select id="district" class="form-control">
  <!-- <option [disabled]="true">Select District/City</option> -->
  <option hidden value="" disabled selected>Select District/City</option>
  <option *ngFor="let postoffice of filterPostOffice" [value]="postoffice.District">
    {{ postoffice.District }}
  </option>
</select>

Placeholder in Select

答案 5 :(得分:0)

这对我来说很好:

<option value="" disabled selected>Text to display</option>
相关问题