从AngularJS中的选定选项中获取值

时间:2016-08-02 00:18:29

标签: javascript angularjs

我正在尝试获取所选选项的url。因此,我的主要目标是将所选选项分配到ng-model

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;

我尝试通过ng-model="ddValue1.url=d.url"

进行分配

2 个答案:

答案 0 :(得分:1)

像这样使用ng-options

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

然后您可以简单地引用ddValue1.url

答案 1 :(得分:0)

ng-repeat中存在拼写错误。调用ddOptions2

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