如何将所选 ng-options 选项的名称更改为自定义名称?

How can i change the selected ng-options option's name to something custom?

下面的代码会将选项名称做成id。

<select ng-model="vm.ships" ng-change="vm.updateShip()"  data-ng-options="ship as ship._id for ship in vm.readyships">

问:如何使名称成为 "ship ship.id is ready ( ship.to) "?

甚至可以用 ng-options 做吗?

想要的结果:

<option value="0" selected="selected" label="57e261">ship 57e261 is ready (sweden) </option>

更新:

变量ship.to可用

有可能,如果 to 是您的 array 的一部分。

ng-options="obj as obj.to for obj in results"

您已经在 data-ng-options 的标签部分完成了您想要的操作。为了获得完整的标签,您可以连接附加文本:

<select ng-model="vm.ships" ng-change="vm.updateShip()"  data-ng-options="ship as ('ship' + ship._id + ' is ready (' + ship.to + ')') for ship in vm.readyships">

除了文档之外,此 previous answer 总结了 ng-options 的不同选项。

假设 ship.to 存在:

<select ng-model="vm.ships" ng-change="vm.updateShip()" data-ng-options="ship as ship._id + ' is ready (' + ship.to + ')' for ship in vm.readyships">