Get current value when change select option - Angular2

前端 未结 6 1497
走了就别回头了
走了就别回头了 2021-01-31 08:00

I\'m writing an angular2 component and am facing this problem.

Description: I want to push an option value in select selector to its handler when the

相关标签:
6条回答
  • 2021-01-31 08:33

    Template:

    <select class="randomClass" id="randomId" (change) = 
    "filterSelected($event.target.value)">
    
    <option *ngFor = 'let type of filterTypes' [value]='type.value'>{{type.display}} 
    </option>
    
    </select>
    

    Component:

    public filterTypes = [{
      value : 'New', display : 'Open'
    },
    {
      value : 'Closed', display : 'Closed'
    }]
    
    
    filterSelected(selectedValue:string){
      console.log('selected value= '+selectedValue)
    
    }
    
    0 讨论(0)
  • 2021-01-31 08:36

    For me, passing ($event.target.value) as suggested by @microniks did not work. What worked was ($event.value) instead. I am using Angular 4.2.x and Angular Material 2

    <select (change)="onItemChange($event.value)">
        <option *ngFor="#value of values" [value]="value.key">
           {{value.value}}
        </option>
    </select>
    
    0 讨论(0)
  • 2021-01-31 08:43

    There is a way to get the value from different options. check this plunker

    component.html

     <select class="form-control" #t (change)="callType(t.value)">
      <option *ngFor="#type of types" [value]="type">{{type}}</option>
    </select>
    

    component.ts

        this.types = [ 'type1', 'type2', 'type3' ];
       this.order = {
          type: 'type1'          
      };  
    
      callType(value){
        console.log(value);
        this.order.type=value;
      }
    
    0 讨论(0)
  • 2021-01-31 08:49

    You can Use ngValue. ngValue passes sting and object both.

    Pass as Object:

    <select (change)="onItemChange($event.value)">
        <option *ngFor="#obj of arr" [ngValue]="obj.value">
           {{obj.value}}
        </option>
    </select>
    

    Pass as String:

    <select (change)="onItemChange($event.value)">
        <option *ngFor="#obj of arr" [ngValue]="obj">
           {{obj.value}}
        </option>
    </select>
    
    0 讨论(0)
  • 2021-01-31 08:50

    In angular 4, this worked for me

    template.html

    <select (change)="filterChanged($event.target.value)">
    <option *ngFor="let type of filterTypes" [value]="type.value">{{type.display}}
    </option>
    </select>
    

    component.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() {
      }
    
      }
    
    0 讨论(0)
  • 2021-01-31 08:53

    Checkout this working Plunker

    <select (change)="onItemChange($event.target.value)">
        <option *ngFor="#value of values" [value]="value.key">{{value.value}}</option>
    </select>
    
    0 讨论(0)
提交回复
热议问题