2016-11-16 230 views
1
@Component({ 
    selector: 'filter', 
    template: "<select [(ngModel)]="filterState" (change)="selected()"> 
<option value="">All</option> 
       <option *ngFor="let s of states " [ngValue]="s">{{ s.label}}</option> 
      </select>", 
}); 

export class FilterComponent { 
    private states = [ 
     { 
     value: 'active', 
     label: 'Active', 
     }, 
     { 
     value: 'done', 
     label: 'Done', 
     }, 
     { 
     value: 'removed', 
     label: 'REMOVED', 
     } 
    ]; 

    private filterState   = ''; 
    selected() :void { 
    //this.filterState is still the initiated value 
    } 
} 

在上述情況下,「全部」選項不顯示,並且每當更改選項ngModel它都不會更新。angular2選擇選項選擇問題

嘗試使用值而不是ngValue,並嘗試使用私有filterState = 0;但同樣的情況,在這裏

回答

1

我想你需要

[ngValue]="" 

,而不是混合value[ngValue]

[value]="s"不會起作用,因爲[value]只支持字符串,但沒有對象。

+0

仍然是同樣的問題 – Niyaz

+0

你能提供一個允許重現的Plunker嗎? –

+0

https://plnkr.co/edit/9ZWpccOyNklxHFL92PT8 – Niyaz