-1

I'm having an issue getting just the value from a dropdown. I have the following structure:

$scope.categories = [
  {id: 7, title: 'Peanut Butter'},
  {id: 4, title: 'Mustard'},
  {id: 3, title: 'Jelly'},
  {id: 2, title: 'Jam'},
  {id: 1, title: 'Mint Jelly'}
];

and I'm creating a dropdown as follows:

<select class="form-control" ng-model="testme.id" ng-options="c as c.title for c in categories track by c.id">
    <option value="">------</option>
</select>
<input type="text" ng-model="testme.pos" />

The dropdown generates fine

For example, if I put in "3" into "testme.pos" and select "Jam", but the model "testme" comes with the following example result:

{id: {id: 2, title: 'Jam'}, pos: 3 },

What I would like to see:

{id: 2, pos: 3}

Any help would be great. I created a plnkr: Select Help

KingFish
  • 8,773
  • 12
  • 53
  • 81
  • 1
    https://docs.angularjs.org/api/ng/directive/ngOptions: `ng-options="c.id as c.title for c in categories"` – JB Nizet Aug 01 '19 at 05:29
  • No, this is not the answer I'm looking for. I'm aware of the docs and, as the plunker I linked to shows, it works. The problem is I want to set the id only. If the position is already filled in, changing the dropdown will override that value. This is why I only want to set the id. – KingFish Aug 01 '19 at 23:37
  • And the docs, and the snippet I pasted right after the link, show how to do just that. – JB Nizet Aug 02 '19 at 05:35

1 Answers1

0

Instead of ng-model="testme.id" use ng-model="testme"

Mike Doe
  • 16,349
  • 11
  • 65
  • 88
jveri27
  • 1
  • 1
  • Thank you, but unfortunately this is not the answer I'm looking for. As the plunker I linked to shows, it works. The problem is I want to set the id only. If the position is already filled in, changing the dropdown will override that value. This is why I only want to set the id. Thank you though for your help – KingFish Aug 01 '19 at 23:41
  • try this, it will not override the position value on dropdown change. ` – jveri27 Aug 02 '19 at 02:08