Angular 4选择整个对象

标签 angular drop-down-menu

我有以下内容:

<select [(ngModel)]="selectedMetric">
        <option *ngFor="let metric of suggestedMetrics" [value]="metric">{{metric.displayName}}</option>
</select>

其中 suggestedMetrics 是一个对象数组(typescript 类)。

当我从列表中选择一个项目时,模型会更新为字符串 "[object Object]" 而不是对象本身。

它只支持字符串吗?我不能将它设置为整个对象吗?这将使我免于在原始数组中再次找到它...

最佳答案

您应该使用 ngValue 而不是 value

  <option *ngFor="let metric of suggestedMetrics" [ngValue]="metric">{{metric.displayName}}</option>

关于Angular 4选择整个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46544375/

相关文章:

Angular 错误 : Please add a @NgModule annotation

angular - 使用可移动输入/标签搜索 (Ionic 2)

Angular 搜索选项自动完成多个字段

ios - 有没有办法在 SwiftUI 中创建下拉菜单/按钮?

c# - 将 DropDownList 绑定(bind)到 ListItemCollection 并且值未添加到 DDL

html - 在现有导航栏中创建下拉菜单

angular - 当 'application/json' 在 Angular4 cli 的 header 中设置时,Http.post 不起作用

typescript - Angular2 Typescript 将 Firebase JSON 转换为对象

Firefox 中的 <SELECT> 不再支持 CSS 字体系列?

php - 使用 MySQL 表填充的 PHP 下拉列表将选择插入到另一个表中