如何在 Angular 2 中的 select 控件中显示占位符(空选项)?

How to show placeholder (empty option) in select control in Angular 2?

我的模板中有这段代码:

<select [ngModel]="selectedSubSectionId" (ngModelChange)="onSubSectionChange($event)">
  <option *ngFor="let subSection of event.subSections" [ngValue]="subSection.id">{{ subSection.name }}</option>
</select>

在我的组件中:

public selectedSubSectionId: any;

public onSubSectionChange(subSectionId: any) {
  // some code I execute after ngModel changes.
}

这工作正常,但一开始我有一个空盒子。我想在那里显示占位符消息。 如何使用 ngModel 执行此操作?

试试这个代码:

<select [ngModel]="selectedSubSectionId? selectedSubSectionId : ''" (ngModelChange)="onSubSectionChange($event)">
   <option value="" disabled selected hidden>Placeholder</option>
   <option *ngFor="let subSection of event.subSections" [value]="subSection.id">{{ subSection.name }}</option>
</select>

我的解决方案:

在组件打字稿文件中,我添加了一个 属性 未初始化的 selectUndefinedOptionValue,并在 html 中添加了 undefinedSelectOptionValue 作为占位符选项的值。此解决方案适用于数字和字符串模型属性。

@Component({
  selector: 'some-component-selector',
  templateUrl:'url to template',
})
export class SomeComponent implements OnInit {
    private selectUndefinedOptionValue:any;
    private someObject:SomeObject;
    
    ngOnInit() {
      someObject = new SomeObject();
    }
}
<select [(ngModel)]="someObject.somePropertyId">
  <option disabled hidden [value]="selectUndefinedOptionValue">-- select --</option>
  <option *ngFor="let option of options" [value]="option.id">option.text</option>
</select>

添加空选项并设置为'undefined'也可以为空值添加

<select [(ngModel)]="barcode">
  <option value="undefined" disabled selected hidden>Select</option>
  <option value="null" disabled selected hidden>Select</option>
  <option *ngFor="let city of turkiye" [ngValue]="city.id">{{city.name}}</option>
</select>

我也有同样的问题,我在这个很棒的网站上找到了一个例子:Angular Quick Tip

另外,我把例子放在下面:

// template
<form #f="ngForm">
  <select name="state" [ngModel]="state">
    <option [ngValue]="null">Choose a state</option>
    <option *ngFor="let state of states" [ngValue]="state">
      {{ state.name }}
    </option>
  </select>
</form>

//component
state = null;

states = [
  {name: 'Arizona', code: 'AZ'},
  {name: 'California', code: 'CA'},
  {name: 'Colorado', code: 'CO'}
];

也适用于 Reactive Forms,这就是我正在使用的:

// template
<div class="form-group">
  <select formControlName="category">
    <option [ngValue]="null">Select Category</option>
    <option *ngFor="let option of options" 
            [ngValue]="option">{{option.label}}</option>
  </select>
</div>

// component
options = [{ id: 1, label: 'Category One' }, { id: 2, label: 'Category Two' }];

form = new FormGroup({
  category: new FormControl(null, Validators.required)
});

感谢Netanel Basal提供答案

这对我有用:

<option [selected]="真" value="">Please select</option>

  1. 添加一个 option 标签 `,内容请 select。
  2. 添加 [selected]="true" 属性。
  3. 添加 value = "" 作为属性。

如果没有 value="",则在您单击 select 字段之前不会显示内容。

对于使用 Bootstrap 4+

的用户

您可以将 [selected]="true" 更改为 selected

<option selected value="">Please select</option>