选择中的Angular 2更改选项

时间:2017-09-15 11:02:17

标签: angular typescript angular2-forms

我有两个选择:

 <form name="newDocumentForm">
<label for="companySelect"> Company: </label>
<select name="companySelect" id="companySelect" [(ngModel)]="companySelect" 
 (ngModelChange)="onChange($event)">
  <option [ngValue]="undefined" disabled selected>Select...</option>
  <option *ngFor="let company of companies" [ngValue]="company">{{company.name}}</option>
</select>

<label for="documentTypeSelect"> Type: </label>
<select name="documentTypeSelect" id="documentTypeSelect" 
 [(ngModel)]="documentTypeSelect">
  <option [ngValue]="undefined" disabled selected>Select...</option>
  <option value="aaa">ty</option>
  <option value="bbbb">ew</option>
</select>
</form>

每当我在第一个选择中更改内容时,我应该在onChange($ event)中添加什么来将documentTypeSelect选项设置为“Select ...”? 像this.documentTypeSelect = ???

这样的东西

1 个答案:

答案 0 :(得分:1)

更改

<option [ngValue]="undefined" 

<option [ngValue]="''"

如果您将companySelect设置为'',则会选择此选项

companySelect:string = '';

对于更复杂的方案,另请参阅Angular 2 Dropdown Options Default Value