2016-11-16 236 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