【发布时间】:2020-01-07 16:07:12
【问题描述】:
我有一个下拉列表,其中包含来自循环的一些选项,我需要在单击按钮时获取选定的选项(不是值)。我尝试使用ngModel,但我能够获得价值,而不是选项。谁能帮帮我吗?下面是代码
app.component.html
<div>
<select>
<option *ngFor="let group of groups" value="{{group.id}}">{{group.name}}</option>
</select>
</div>
<input type="button" (click)="getVal()" value="submit"/>
app.component.ts
import { Component, ViewChild, ElementRef } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
ngOnInit() {
}
getVal() {
}
groups = [{
"id": 1,
"name": "pencils",
"items": "red pencil"
},
{
"id": 2,
"name": "rubbers",
"items": "big rubber"
},
{
"id": 3,
"name": "rubbers1",
"items": "big rubber1"
}];
}
【问题讨论】:
-
“我需要获取选定的选项(不是值)”到底是什么意思?你想要
name吗? -
是的,我需要名字
-
使用
indexOf()方法查找您选择的选项 -
我知道我也在使用值