【问题标题】:angular ng-option Option and Labelangular ng-option 选项和标签
【发布时间】:2014-10-12 10:00:18
【问题描述】:

我有一个选择字段:

<select
    id="aircraft_id"
    data-id="{{( $obj->exists ) ? $obj->aircraft_id : NULL}}"
    data-placeholder="Wähle ein Flugzeug"
    no-results-text="'Konnte kein Flugzeug finden'"
    class="form-control"
    chosen="directiveOptions"
    name="aircraft_id"
    ng-model="aircraft"
    ng-change="checkSeats(aircraft.id)"
    ng-disabled="newAircraftIdentifier.length > 0 || newAircraftModel.length > 0"
    ng-options="aircraft.ident for aircraft in aircrafts">
</select>

和“飞机”json对象包含:

[Object { ident="--", id="", seats=""}, 
 Object { id=1, ident="D-3417", seats=2}, 
 Object { id=2, ident="D-1021", seats=2}, 
 Object { id=3, ident="D-4582", seats=1}, 
 Object { id=5, ident="D-FTSD", seats=null}, 
 Object { id=10, ident="D-5970", seats=1}
]

除了我在选择中的选项是我的 json 对象的索引,而不是 id 之外,一切都有效...

所以结果如下:

<option value="0" selected="selected">--</option>
<option value="1">D-3417</option>
<option value="2">D-1021</option>
<option value="3">D-4582</option>
<option value="4" selected="selected">D-FTSD</option>
<option value="5">D-5970</option>

但我希望选项值是我在数组中的 ID,如最后一项中的 ID 10,而不是 5... 希望你明白我的意思。

我认为错误的行是这样的:

ng-options="aircraft.ident for aircraft in aircrafts"

但我不明白我如何解决这个问题。

提前致谢,

雷夫

【问题讨论】:

  • aircraft.ident for aircraft in aircrafts track by aircraft.id 应该可以工作
  • 谢谢...现在可以工作了...通过飞机跟踪.id 是解决方案...

标签: javascript json angularjs


【解决方案1】:

试试

aircraft.id as aircraft.ident for aircraft in aircrafts

【讨论】:

  • 我之前尝试过,但它不起作用......我找到了解决方案:aircraft.ident for aircraft in aircrafts track by aircraft.id
【解决方案2】:

您不应该关心option 元素上的value,因为它与Angularjs 模型无关。使用 ng-options 创建的 DOM 将始终具有从 0..n 开始的值,请检查您的 $scope.aircraft 模型以获取实际选择的值

您也可以将其用作 aircraft.id as aircraft.ident for aircraft in aircrafts,然后模型 $scope.aircraft 将仅更新为 id

【讨论】:

    猜你喜欢
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多