2017-10-10 63 views
0

如何在模板中访问下拉列表的选定值?在模板中访问下拉列表的选定值

<div> 
    <select> 
     <option *ngFor="let client of clients" [value]="client.id">{{ client.name }}</option> 
    </select> 
    </div> 

    <!-- here I can access clients, but not the selected client --> 

回答

1

你可以选择的值这样的:

<div> 
    <select #selectedClient> 
     <option *ngFor="let client of clients" [value]="client.id">{{ client.name }}</option> 
    </select> 
    </div> 

    <!-- here I can access clients, but not the selected client --> 
    <p>{{selectedClient.value}}</p> 

希望它有助于ü:)

+0

它不起作用 – Lev

+0

你的意思是什么不起作用?它给你错误..或什么? –

0

你也可以这样来做:

<div> 
    <select #dropdown (change)="onDropdownSelect(dropdown)"> 
     <option *ngFor="let client of clients" [value]="client.id">{{ client.name }}</option> 
    </select> 
    </div> 

在组件中:

onDropdownSelect(elem){ 
    console.log(elem.value); 
    }