在下拉菜单中有AngularJS正确的文本,但错误值 [英] Dropdown in AngularJS has Correct Text, but Wrong Value

查看:182
本文介绍了在下拉菜单中有AngularJS正确的文本,但错误值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的下拉菜单:

<select class="form-control form-controls input-sm" ng-model="vm.retailer.state" ng-options="state.code as state.name for state in vm.states" required>
    <option value="">-- Select a State --</option>
</select>

下面只是在数据中的第一个状态:

Here's just the first state in the data:

  "State": [
    {
      "code": "AL",
      "name": "Alabama"
    },

下面是什么在HTML渲染:

Here's what's being rendered in the HTML:

<select class="form-control form-controls input-sm ng-pristine ng-invalid ng-invalid-required ng-touched" ng-model="vm.retailer.state" ng-options="state.code as state.name for state in vm.states" required="">
    <option value="" class="">-- Select a State --</option>
    <option value="0" label="Alabama">Alabama</option>
    ...
</select>

我一直在被问同样的问题,或多或少,但没有什么工作其他职位东张西望。我已经设置在加载状态的方法一个破发点,我可以证实,这两个code和名称在数组中被发送到视图。

I've been looking around at other posts that are asking the same question, more or less, but nothing is working. I have set a break point on the method that loads the states, and I can confirm that both Code and Name are in the array being sent to the view.

推荐答案

终于找到了帖子说了正确的答案。我失踪的状态。code轨。这奏效了。

Finally found a post that had the right answer. I was missing "track by state.code". That did the trick.

<select class="form-control form-controls input-sm" ng-model="vm.retailer.state" ng-options="state.code as state.name for state in vm.states track by state.code" required>
    <option value="">-- Select a State --</option>
</select>

这篇关于在下拉菜单中有AngularJS正确的文本,但错误值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆