gpt4 book ai didi

Angular 2问题获取选择选项值

转载 作者:太空狗 更新时间:2023-10-29 17:25:14 27 4
gpt4 key购买 nike

我有一个选择

<select name='shape_id' ngModel (change)='changeShape($event.target.value)'>
<option *ngFor="let shape of shapes" [ngValue]="shape.id">{{shape.name}}</option>
</select>

这是数据。

  shapes = [
{id:'1', name:'Angle'},
{id:'2', name:'Bar'},
];

我无法获取值。

 changeShape(shape){
console.log(shape);
}

这输出“0:1”,但我想要值 1。

这是选项在检查器中的样子。

<option value="0: 1" ng-reflect-ng-value="1">Angle</option>

如何获取id值1?

最佳答案

将 [ngValue] 更改为 [value]

  <select name="shape_id" (change)="changeShape($event.target)">
<option *ngFor="let shape of shapes" [value]="shape.id">
{{shape.name}}
</option>
</select>

并将形状调整为 shape.value。

changeShape(shape){
console.log(shape.value);
}

关于Angular 2问题获取选择选项值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40923928/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com