user4756836
user4756836

Reputation: 1337

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"

Upvotes: 1

Views: 52

Answers (2)

Shakeer Hussain
Shakeer Hussain

Reputation: 2546

There is a typo error in ng-repeat. call ddOptions2

<option ng-repeat="d  in ddOptions2" value="{{d.value}}">{{d.text}}</option>

Upvotes: 0

Phil
Phil

Reputation: 165070

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

Upvotes: 1

Related Questions