Angular + Ionic, ng-options implement value tag in

2019-09-17 20:18发布

问题:

I am using ng-options on a <select> like this ng-options="item.model for item in prodataSelect | unique:'model' "

But I would like the <value="" tag of the generated options to contain the item.id instead of the autoincremental value use by ng-options.

How to achieve this ?

HTML:

<select id = "chosenmodel" class="button button-full button-dark" ng-model="selectedModel" ng-options="item.model for item in prodataSelect | unique:'model' "> 
  <option value="">Select a Model</option>
</select>

<a id="submitbutton" class="button button-block button-dark ui-btn ui-btn-b ui-corner-all" ng-click='launchCompute()' href="#/app/compute/result">Compute my board</a>

Each item in prodataSelect is an object :

{
  brand: "John",
  fun: "0",
  id: 1
},
{
  brand: "you",
  fun: "0",
  id: 185
},
{
  brand: "Ark",
  fun: "0",
  id: 17
},

回答1:

use below syntax

       `ng-options="item.id as item.model for item in prodataSelect"`

read more about it here



回答2:

Use "track by item.id" to set option value to id

ng-options="item.id as item.model for item in prodataSelect track by item.id"