Angular selected="selected" 不使用 [(ngModel)]

Angular selected="selected" not working with [(ngModel)]

selected = 如果与 [(ngModel)]

一起使用,“selected”将不起作用

找到下面的代码:

HTML :

<div>
  <select class="dropdownlist" [(ngModel)]="DeviceModel" name="GensetModel">
    <option value="" selected="selected" disabled="disabled">test</option>
    <option *ngFor="let deviceModel of DeviceModelList" [value]="deviceModel">
      {{deviceModel}}
    </option>
  </select>
</div>

<p>You selected: {{DeviceModel}}</p>





TS :

 private _DeviceModel: string ;
  public get DeviceModel(): string {
    return this._DeviceModel;
  }
  public set DeviceModel(v: string) {
    this._DeviceModel = v;
  }
  // DeviceModel = '';
  DeviceModelList = ['option1', 'option2', 'option3'];

我希望 test 在页面加载后成为占位符。

我不知道我的代码有什么问题?

谢谢

将您的 _DeviceModel 初始化为空字符串:

private _DeviceModel: string = '';

这是一个展示它的 stcakblitz:

https://stackblitz.com/edit/angular-2qybir?file=src%2Fapp%2Fapp.component.html