AngularJS 中的下拉菜单文本正确,但值错误
Dropdown in AngularJS has Correct Text, but Wrong Value
这是我的下拉菜单:
<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>
这里只是数据中的第一个状态:
"State": [
{
"code": "AL",
"name": "Alabama"
},
这是 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 和 Name 都在发送到视图的数组中。
终于找到了 post 的正确答案。我失踪了 "track by state.code"。成功了。
<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>
这是我的下拉菜单:
<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>
这里只是数据中的第一个状态:
"State": [
{
"code": "AL",
"name": "Alabama"
},
这是 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 和 Name 都在发送到视图的数组中。
终于找到了 post 的正确答案。我失踪了 "track by state.code"。成功了。
<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>