2017-05-06 77 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