Angular5的select触发change事件的时候,怎么拿到当前option的对象集合?

土货
  • 711

假设数据是这样的。。objList = [{name:"jack",age:23},{name:"lucy",24}]

<select class="form-control" (change)="selected($event)">
<option *ngFor="let obj of objList" value="{{obj.Value}}">{{obj.Name}}</option>
                                                
</select>
selected($event){

      }

选择lucy的option时,selected方法怎么拿到当前option的对象集合?即:{name:"lucy",24}

回复
阅读 4.6k
2 个回答
Dopp
  • 1.1k
<select #select class="form-control" (change)="selected(select.value)">
  <option *ngFor="let obj of objList;let i=index" value="{{i}}">{{obj.name}}</option>
</select>

objList = [{name:"jack",age:23},{name:"lucy",age:24}];
selected(index){
  console.log(this.objList[index])
}

将select的value设置成对应的索引index,拿到索引就可以拿到对应的对象了

let selectedIndex = $event.target.selectedIndex;
let selectedItem = selectedIndex > -1 ? this.objList[selectedIndex] : null;
宣传栏