user494461
user494461

Reputation:

how to set the ng-model to a property of an object

from this example http://jsfiddle.net/qWzTb/

How can I set $scope.correctlySelected directly to the value on selecting in the select box and not to the entire object for e.g. { label: 'one', value: 1 },

So on selecting anything $scope.correctlySelected should be 1 or 2 and not the entire object

{ label: 'one', value: 1 }

or

{ label: 'two', value: 2 }

Upvotes: 1

Views: 65

Answers (2)

Snowman
Snowman

Reputation: 1256

If I understand correctly, you want to be able to just set $scope.correctlySelected = 2;, right? If so then you just need to do this for the ng-options directive:

<select ng-model="correctlySelected" ng-options="opt.value as opt.label for opt in options">

Here's an updated fiddle: http://jsfiddle.net/qWzTb/89/

Upvotes: 0

Ilan Frumer
Ilan Frumer

Reputation: 32357

Here is a working fiddle: http://jsfiddle.net/AXHeA/

This is how you do it:

<select ng-model="selected"
        ng-options="opt.value as opt.label for opt in options">

And then you can assign it like so:

$scope.selected = 2;

Upvotes: 2

Related Questions