2016-12-08 8 views
0

私はselectタグを作成していて、* ngForを使用してselect_options配列からその中にオプションを設定しています。ただし、常に下部に空白のオプション項目が作成されます。それを取り除くのを助けてください。ngForを使用する場合、余分な空白の選択オプションを取り除く方法

たとえば、私は選択肢の配列を持っています: select_options = ['banana'、 'apple'、 'orange'];

<select> 
     <option *ngFor="let fruit of select_options" 
      value="fruit"> 
      {{fruit}} 
     <option> 
    </select> 

答えて

2

これは、閉鎖していないので、<option>です。これを試してみてください:

<select> 
    <option *ngFor="let fruit of select_options" 
     value="fruit"> 
     {{fruit}} 
    </option> 
</select> 

それが動作し、デフォルト値を選択するために、この試みます。@Baconbeastnzで述べたように

<select [(ngModel)]="selectedFruit" name="test"> 

this.selectedFruit = this.select_options[0]; 

を。

+0

ありがとう! –

関連する問題