简体   繁体   English

angular ng-option选项和标签

[英]angular ng-option Option and Label

I have a Select field: 我有一个选择字段:

<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>

and "aircrafts" json object contains: 和“飞机” 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}
]

Everything works except that my option in the select is the index of my json Object, and not the id... 一切正常,除了select中的选项是我的json对象的索引,而不是id ...

so the result is the following: 因此结果如下:

<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>

But I want that the option value is my ID in the array like ID 10 in the last item, and not 5... Hope you understand what I mean. 但是我希望该选项值是我在数组中的ID,就像最后一项中的ID 10,而不是5。希望您理解我的意思。

I think the wrong line is this: 我认为这是错误的行:

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

But I dont understand how I fix this. 但是我不明白我该如何解决。

Thanks in advance, 提前致谢,

Leif 雷夫

尝试

aircraft.id as aircraft.ident for aircraft in aircrafts

You shouldn't be concerned with value on option element as it's irrelevant with Angularjs model. 您不必担心option元素的value ,因为它与Angularjs模型无关。 DOM created with ng-options will have always values from 0..n, check your $scope.aircraft model for actually selected value 使用ng-options创建的DOM的值始终为0..n,请检查$scope.aircraft模型以获取实际选择的值

you can also use it as aircraft.id as aircraft.ident for aircraft in aircrafts then the model $scope.aircraft will be updated with only id 您还可以将其用作aircraft.id as aircraft.ident for aircraft in aircrafts然后将仅使用id更新模型$scope.aircraft

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM