如何在Angular 2中的"select"中获得新选择?

Hon*_*iao 331 typescript angular

我正在使用Angular 2(TypeScript).

我想为新的选择做点什么,但是我在onChange()中得到的东西总是最后选择.我怎样才能获得新的选择?

<select [(ngModel)]="selectedDevice" (change)="onChange($event)">
    <option *ngFor="#i of devices">{{i}}</option>
</select>

onChange($event) {
    console.log(this.selectedDevice);
    // I want to do something here for new selectedDevice, but what I
    // got here is always last selection, not the one I just select.
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*cok 674

如果您不需要双向数据绑定:

<select (change)="onChange($event.target.value)">
    <option *ngFor="let i of devices">{{i}}</option>
</select>

onChange(deviceValue) {
    console.log(deviceValue);
}
Run Code Online (Sandbox Code Playgroud)

对于双向数据绑定,请分隔事件和属性绑定:

<select [ngModel]="selectedDevice" (ngModelChange)="onChange($event)" name="sel2">
    <option [value]="i" *ngFor="let i of devices">{{i}}</option>
</select>
Run Code Online (Sandbox Code Playgroud)
export class AppComponent {
  devices = 'one two three'.split(' ');
  selectedDevice = 'two';
  onChange(newValue) {
    console.log(newValue);
    this.selectedDevice = newValue;
    // ... do other stuff here ...
}
Run Code Online (Sandbox Code Playgroud)

如果devices是对象数组,则绑定到ngValue而不是value:

<select [ngModel]="selectedDeviceObj" (ngModelChange)="onChangeObj($event)" name="sel3">
  <option [ngValue]="i" *ngFor="let i of deviceObjects">{{i.name}}</option>
</select>
{{selectedDeviceObj | json}}
Run Code Online (Sandbox Code Playgroud)
export class AppComponent {
  deviceObjects = [{name: 1}, {name: 2}, {name: 3}];
  selectedDeviceObj = this.deviceObjects[1];
  onChangeObj(newObj) {
    console.log(newObj);
    this.selectedDeviceObj = newObj;
    // ... do other stuff here ...
  }
}
Run Code Online (Sandbox Code Playgroud)

Plunker- 不使用<form>
Plunker- 使用<form>和使用新的表单API

  • $event.target.value 类型“EventTarget”上不存在属性“value”?你能给 typescript 4 一些建议吗 (5认同)
  • @HongboMiao,如果你有一个对象数组,请使用[ngValue].如果您有一个基本类型数组(字符串,布尔值,整数),请使用[value]. (2认同)

Bro*_*cco 38

您可以通过在select标记上创建引用变量将值传递回组件#device,并将其传递给更改处理程序onChange($event, device.value)应具有新值

<select [(ng-model)]="selectedDevice" #device (change)="onChange($event, device.value)">
    <option *ng-for="#i of devices">{{i}}</option>
</select>

onChange($event, deviceValue) {
    console.log(deviceValue);
}
Run Code Online (Sandbox Code Playgroud)

  • 这里是什么卷`[(ngModel)]` (3认同)

rob*_*ing 18

只需使用[ngValue]而不是[value] !!

export class Organisation {
  description: string;
  id: string;
  name: string;
}
export class ScheduleComponent implements OnInit {
  selectedOrg: Organisation;
  orgs: Organisation[] = [];

  constructor(private organisationService: OrganisationService) {}

  get selectedOrgMod() {
    return this.selectedOrg;
  }

  set selectedOrgMod(value) {
    this.selectedOrg = value;
  }
}


<div class="form-group">
      <label for="organisation">Organisation
      <select id="organisation" class="form-control" [(ngModel)]="selectedOrgMod" required>
        <option *ngFor="let org of orgs" [ngValue]="org">{{org.name}}</option>
      </select>
      </label>
</div>
Run Code Online (Sandbox Code Playgroud)


小智 12

我在https://angular.io/docs/ts/latest/guide/forms.html上执行Angular 2表单教程(TypeScript版本)时遇到了这个问题.

选择/选项块不允许通过选择其中一个选项来更改选择的值.

做Mark Markkok建议的工作,虽然我想知道在原始教程中是否遗漏了某些内容,或者是否有更新.在任何情况下,添加

onChange(newVal) {
    this.model.power = newVal;
}
Run Code Online (Sandbox Code Playgroud)

到HeroFormComponent类中的hero-form.component.ts

(change)="onChange($event.target.value)"
Run Code Online (Sandbox Code Playgroud)

<select>元素中的hero-form.component.html 使它工作


Abd*_*leh 12

我有同样的问题,我使用下面的代码解决了:

(change)="onChange($event.target.value)"
Run Code Online (Sandbox Code Playgroud)

  • 这在较新版本的 Angular 中不起作用 (2认同)

val*_*ine 11

在角度 6 及以上使用 selectionChange。例子 (selectionChange)= onChange($event.value)


B.H*_*deh 5

在 Angular 8 中,您可以像这样简单地使用“selectionChange”:

 <mat-select  [(value)]="selectedData" (selectionChange)="onChange()" >
  <mat-option *ngFor="let i of data" [value]="i.ItemID">
  {{i.ItemName}}
  </mat-option>
 </mat-select>
Run Code Online (Sandbox Code Playgroud)

  • 这是一个物质事件 (4认同)

tin*_*one 5

如果您不需要双向数据绑定:

<select (change)="updateSorting($event)">
  <option disabled selected>Sorting</option>
  <option value="pointDes">pointDes</option>
  <option value="timeDes">timeDes</option>
  <option value="timeAsc">timeAsc</option>
  <option value="pointAsc">pointAsc</option>
</select>
Run Code Online (Sandbox Code Playgroud)
updateSorting(e: any) {
  // console.log((e.target as HTMLSelectElement)?.value); // also work
  console.log(e.target.value);
}
Run Code Online (Sandbox Code Playgroud)