简体   繁体   English

角度选择保持生成默认缺失选项

[英]Angular select keeps generating default missing option

I have a select being rendered inside of an agGrid . 我有一个selectagGrid内部渲染。 Nothing exceptionally complicated. 没有什么特别复杂的。

<select ng-model="data.stdPayerClassFnbr" ng-change="vm.save(data)" style="width: 100%"
        ng-options="item.stdPayerClass for item in vm.payerClassList track by item.id">
</select>

The crazy thing is this; 疯狂的是这个; I keep getting the missing value option: 我一直在获取缺失值选项:

<option value="?" selected="selected"></option>

Even though the value of data.stdPayerClassFnbr exists in the list of rendered options: 即使data.stdPayerClassFnbr的值存在于呈现选项列表中:

<option value="1" label="TBD">TBD</option>

The underlying data type of both data.stdPayerClassFnbr and item.id is int ; data.stdPayerClassFnbritem.id的基础数据类型是int ; however I've tried making them both string by executing a map against their values before binding them. 但是我已经尝试通过在绑定它们之前对它们的值执行map来使它们都成为string That did not help. 这没有用。 I've got to be configuring this select wrong. 我必须配置这个select错误。

I've also tried the ng-repeat approach: 我也尝试过ng-repeat方法:

<select ng-model="data.stdPayerClassFnbr" ng-change="vm.save(data)" style="width: 100%">
    <option ng-repeat="item in vm.payerClassList" value="{{item.id}}">{{item.stdPayerClass}}</option>
</select>

This did not help either. 这也没有帮助。

Make sure your stdPayerClassFnbr model object has the same properties as the payerClassList[x] items. 确保stdPayerClassFnbr模型对象具有与payerClassList[x]项相同的属性。 Especially properties id and stdPayerClass 特别是属性idstdPayerClass

I found the issue. 我发现了这个问题。 It was the track by expression. 这是表达的track by Since I was binding to an array of objects that came back from a lookup, when selecting or looking for a value, it was looking for the entire object by reference. 由于我绑定到从查找返回的对象数组,因此在选择或查找值时,它通过引用查找整个对象。 Removing the track by expression caused the binding to work directly on the numeric value: track by表达式删除track by导致绑定直接对数值起作用:

<select ng-model="data.stdPayerClassFnbr" ng-change="vm.save(data)" style="width: 100%"
        ng-options="item.id as item.stdPayerClass for item in vm.payerClassList">
</select>

This generates a very different list of options: 这会生成一个非常不同的选项列表:

<option value="number:1" label="TBD" selected="selected">TBD</option>

I hope this helps somebody in the future! 我希望这有助于将来的某些人! It may even help me one day! 它甚至可能有一天帮助我!

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

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