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

Lea*_*xxx 22 angular2-ngmodel angular

我在我的模板中有这个代码:

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

在我的组件中:

public selectedSubSectionId: any;

public onSubSectionChange(subSectionId: any) {
  // some code I execute after ngModel changes.
}
Run Code Online (Sandbox Code Playgroud)

这样可以,但最初我有一个空盒子.我想在那里显示占位符消息.如何使用ngModel执行此操作?

MeT*_*TTe 32

我的解决方案

在组件typescript文件中,我添加了一个我没有初始化的属性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();
    }
}
Run Code Online (Sandbox Code Playgroud)
<select [(ngModel)]="someObject.somePropertyId">
  <option disabled hidden [value]="selectUndefinedOptionValue">-- select --</option>
  <option *ngFor="let option of options" [value]="option.id">option.text</option>
</select>
Run Code Online (Sandbox Code Playgroud)

  • 它也适用于[value] ="undefined". (15认同)
  • 适用于Chrome 60.0.3112.113,带有<option [ngValue] = null> </ option> (4认同)

小智 11

我有同样的问题,我在这个伟大的网站上找到了一个例子: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'}
];
Run Code Online (Sandbox Code Playgroud)

也适用于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)
});
Run Code Online (Sandbox Code Playgroud)

感谢Netanel Basal提供的答案

  • 我建议将 `disabled` 属性添加到 `&lt;option&gt;`。我已经验证这在 IE11 上有效。 (2认同)

Ser*_*KCI 9

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

<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>
Run Code Online (Sandbox Code Playgroud)


Rod*_*kin 5

试试这段代码:

<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>
Run Code Online (Sandbox Code Playgroud)