使用新创建的Angular 11项目。
app.component.html
<div *ngFor="let i of getItems()">
  <select (change)="onSelectionChange($event.target.value)">
    <option></option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
  </select>
</div>app.component.ts
export class AppComponent {
  
  getItems(): any[] {
    return [{a: 1}, {a: 2}, {a: 3}, {a: 4}];
  }
  onSelectionChange(event) {
    console.log(event);
  }
}一切似乎都很好。四个选择框可见,每个框在下拉列表中都有一个空值和四个整数。奇怪的是,当我打开一个下拉列表并单击以选择一个值时,onSelectionChanged()事件会被触发,但select中的选定值不会改变。
如果我将getItems()方法更改为返回普通数字而不是对象,则选择组件将按预期开始工作,如下所示:
getItems(): any[] {
  return [1, 2, 3, 4];
}此外,我还获得了从HTML标记中删除(change)事件的正确行为(在这种情况下,该事件不会被触发)。
我正在使用这些依赖项
"dependencies": {
  "@angular/animations": "~11.0.1",
  "@angular/common": "~11.0.1",
  "@angular/compiler": "~11.0.1",
  "@angular/core": "~11.0.1",
  "@angular/forms": "~11.0.1",
  "@angular/platform-browser": "~11.0.1",
  "@angular/platform-browser-dynamic": "~11.0.1",
  "@angular/router": "~11.0.1",
  "rxjs": "~6.6.0",
  "tslib": "^2.0.0",
  "zone.js": "~0.10.2"
},
"devDependencies": {
  "@angular-devkit/build-angular": "~0.1100.2",
  "@angular/cli": "~11.0.2",
  "@angular/compiler-cli": "~11.0.1",
  "@types/jasmine": "~3.6.0",
  "@types/node": "^12.11.1",
  "codelyzer": "^6.0.0",
  "jasmine-core": "~3.6.0",
  "jasmine-spec-reporter": "~5.0.0",
  "karma": "~5.1.0",
  "karma-chrome-launcher": "~3.1.0",
  "karma-coverage": "~2.0.3",
  "karma-jasmine": "~4.0.0",
  "karma-jasmine-html-reporter": "^1.5.0",
  "protractor": "~7.0.0",
  "ts-node": "~8.3.0",
  "tslint": "~6.1.0",
  "typescript": "~4.0.2"
}发布于 2021-01-02 22:11:20
它与你的角度版本或依赖关系无关,你的选项和选择值应该等于select菜单中的显示值,现在你的值是object,你的选项是number
https://stackoverflow.com/questions/65538907
复制相似问题