更改选择选项 - Angular2时获取当前值

Gar*_*rry 33 html angular

我正在写一个angular2组件,我正面临着这个问题.

描述:我想在(change)触发事件时将select选择器中的选项值推送到其处理程序.

如下面的模板:

<select (change)="onItemChange(<!--something to push-->)">
    <option *ngFor="#value of values" value="{{value.key}}">{{value.value}}</option>
</select>
Run Code Online (Sandbox Code Playgroud)

组件类:

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
  }
}
Run Code Online (Sandbox Code Playgroud)

如何获取值(不使用jquery)?

Mub*_*hir 58

有一种方法可以从不同的选项中获取价值.检查这个plunker

component.html

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

component.ts

    this.types = [ 'type1', 'type2', 'type3' ];
   this.order = {
      type: 'type1'          
  };  

  callType(value){
    console.log(value);
    this.order.type=value;
  }
Run Code Online (Sandbox Code Playgroud)

  • 这个代码在这里有什么用处this.order = {type:'type1'}; (2认同)

jav*_*ved 26

角度4,这对我有用

template.html

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

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() {
  }

  }
Run Code Online (Sandbox Code Playgroud)


mic*_*yks 5

检出此工作柱塞

<select (change)="onItemChange($event.target.value)">
    <option *ngFor="#value of values" [value]="value.key">{{value.value}}</option>
</select>
Run Code Online (Sandbox Code Playgroud)