Estoy escribiendo un componente angular2 y me enfrento a este problema.
Descripción: quiero enviar un valor de opción en el selector de selección a su controlador cuando se activa el evento (change) .
Como la siguiente plantilla:
<select (change)="onItemChange(<!--something to push-->)"> <option *ngFor="#value of values" value="{{value.key}}">{{value.value}}</option> </select>Clase de componente:
export Class Demo{ private values = [ { key:"key1", value:"value1" }, { key:"key2", value:"value2" } ] constructor(){} onItemChange(anyThing:any){ console.log(anyThing); //Here I want the changed value } }¿Cómo puedo obtener el valor (sin usar jquery)?
Hay una manera de obtener el valor de diferentes opciones. revisa este plunker
componente.html
<select class="form-control" #t (change)="callType(t.value)"> <option *ngFor="#type of types" [value]="type">{{type}}</option> </select>componente.ts
this.types = [ 'type1', 'type2', 'type3' ]; callType(value) { console.log(value); this.order.type = value; }En angular 4, esto funcionó para mí.
plantilla.html
<select (change)="filterChanged($event.target.value)"> <option *ngFor="let type of filterTypes" [value]="type.value">{{type.display}} </option> </select>componente.ts
export class FilterComponent implements OnInit { selectedFilter:string; public filterTypes = [ { value: 'percentage', display: 'percentage' }, { value: 'amount', display: 'amount' } ]; constructor() { this.selectedFilter = 'percentage'; } filterChanged(selectedValue:string){ console.log('value is ', selectedValue); } ngOnInit() { } }