user4756836 user4756836 - 4 months ago 7
AngularJS Question

Getting values from the selected option in AngularJS

I am trying to get the

url
of the selected option. So my main goal is to assign the selected option to the
ng-model
of the
select


HTML:

<select ng-model="ddValue1.value">
<option ng-repeat="d in ddOptions1" value="{{d.value}}">{{d.text}}</option>
</select>


JS:

$scope.ddOptions2 = [
{value: 'value1', text: 'Text1', url: 'google.com'},
{value: 'value2', text: 'Text2', url: 'yahoo.com'}
];
$scope.ddValue2 = {};
$scope.ddValue2.value = $scope.ddOptions2[0].value;


I tried assigning it by doing
ng-model="ddValue1.url=d.url"

Answer

Use ng-options like this

<select ng-model="ddValue1"
    ng-options="d.text for d in ddOptions1 track by d.value">
</select>

Then you can simply reference ddValue1.url