2016-08-15 102 views
0

我在選擇框的問題angularjsAngularJS選擇框

我得到使用這個我locationList:

locationFactory.getLocation().then(function (data) { 
     $scope.locationList = data.data.locationList; 
    }); 

,結果是這樣的:Result

這是怎麼我將它添加到視圖中的選擇框中:

<select ng-options="location.strBarangay for location in locationList" 
             ng-model="details.location" id="crOrderLoc"> 
           </select> 

我的問題是,無論何時我選擇op它總是有一個空白選項,它具有我的json中第一個對象的值。 實施例:我有一個JSON對象[{1},{2},{3}] 而在我的選擇它將安排是這樣的:

  1. 選項值= 1(它會顯示空)
  2. 選項值= 2(它會顯示1)
  3. 選項值= 3(它會顯示2)

我嘗試這樣設置我的模型的值中選擇

$scope.details.location = { 
     dblLocationPrice: $scope.locationList[0].dblLocationPrice, 
     intLocationID: $scope.locationList[0].intLocationID, 
     intLocationStatus: $scope.locationList[0].intLocationStatus, 
     strBarangay: $scope.locationList[0].strBarangay, 
     strCity: $scope.locationList[0].strCity 
    }; 

但有一個錯誤: angular.min.js:117類型錯誤:未定義 在新prodSalesCtrl

無法讀取屬性「0」,但它是在這裏工作http://jsfiddle.net/MTfRD/3/(編輯)

但它不是在工作我選擇

回答

1

您需要加載數據後設置選擇的默認值:

locationFactory.getLocation().then(function (data) { 
    $scope.locationList = data.data.locationList; 
    if($scope.locationList && $scope.locationList.length>0) 
     $scope.details.location = $scope.locationList[0]; 
}); 
+0

它的工作。非常感謝你。 對不起,我不能給你+代表。我在這裏還是新手 – jjjjjj