2017-05-06 76 views
0

我正在使用AngularJS 1.5.6。 ng-app和ng-controller正确设置。我的选择是AngularJS不选择值

$scope.apis =[ 
      {'id':0, 'apiUrl':'https://api0.com', 'label':'Label0'}, 
      {'id':1, 'apiUrl':'https://api1.com', 'label':'Label1'}, 
      {'id':2, 'apiUrl':'https://api2.com', 'label':'Label2'}, 
      {'id':3, 'apiUrl':'https://api3.com','label':'Label3'} 
     ]; 

$scope.selectedApi = $scope.apis[1];// default 

在我选择的标签,如果我把它声明为:

<select ng-model="selectedApi" ng-options="x.apiUrl as x.label for x in apis"> 

然后每个选项呈现这样的(例如,Label1的):

<option label="Label1" value="string:https://api1.com">Label1</option> 

但值没有被选作我console.log()我的选择在一个事件处理程序中,它显示选中的值是undefined。是什么原因?

+1

* AngularJS 3.2.1 *:有没有这样的事情。 –

+1

由于ng-options是'x.apiUrl as ...',所选值应该是apiUrl,而不是api。如果你希望模型(即selectedApi)是api,那么ng-options应该是'x as ...'。 –

回答

0

我想你的意思:

<select ng-model="selectedApi" ng-options="x as x.label for x in apis"></select> 

在这种情况下selectedApi是一个对象(关于初始状态)

Demo in Plunkr