javascriptangularjsng-optionsangularjs-ng-optionsangularjs-select

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


The following code will do the option name to the id.

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

Q: How can i make the name something to be "ship ship.id is ready ( ship.to) " ?

is that even possible to do with ng-options?

wanted result:

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

UPDATE:

variable ship.to is avaliable


Solution

  • You are already doing what you want in the label portion of the data-ng-options. In order to get the full label you can concatenate on the additional text:

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

    In addition to the documentation, this previous answer sums up the different options you have with ng-options.