下拉列表中的选定值不起作用

时间:2017-10-19 14:02:25

标签: angular select

我想在组件类的下拉列表中使用所选值。 当从下拉列表中选择一个值时,我使用ngModel存储该值,但它不起作用,也尝试了另一种方法,即我触发了一个事件函数并将所选值作为参数传递,但这种方法也不起作用。

<select [(ngModel)]="selectedControl" class="form-control" (click)="onSelection($event)">
          <option *ngFor="let control of controlArray; let i=index" 
          [value]="control">{{controlArray[i].name}}</option>                                              
</select> 

当我在控制台上输出所选值时,它没有显示,这里是组件类代码:

onSelection(control){  
    console.log(typeof(control))
    console.log(control.target.value)
  }

请帮忙。

1 个答案:

答案 0 :(得分:0)

您应该使用change事件,而不是click

<select [(ngModel)]="selectedControl" class="form-control" (change)="onSelection()">
  <option *ngFor="let control of controlArray; let i=index"
          [ngValue]="control">{{controlArray[i].name}}</option>
</select>

另外,您应该检查selectedControl当前选择的值。

onSelection(){
  console.log(this.selectedControl)
}

最后一件事 - 如果您想这样做或者使用ngValue,您应该设置一个标量值。