2015-06-17 52 views
0

在表單中我有一個帶多項選擇的下拉列表,如下所示。元素從數據庫加載。AngularJS:在標籤中設置之前選擇的選項<select>

<label>Items</label> 

<select class="form-control m-b" 
     ng-model="model.livingComplexId" 
     x-ng-change="updateOne(model.livingComplexId)"> 

    <option></option> 
    <option ng-repeat="itemOne in itemsForAddrLevelOne" 
      value="{{itemOne.id}}">{{itemOne.tobName}}</option> 
</select> 

enter image description here

我做出選擇。

enter image description here

例如,我選擇一個item2

然後保存數據。然後,我打開表單編輯,我想看看,我選擇了一個項目,但該列表是空的...

enter image description here

如何設置之前選擇的價值?

我將非常感激這些信息。謝謝大家。

回答

1

我會建議使用ngOptions指令生成選擇菜單選項:

<select ng-model="model.livingComplexId" ng-options="itemOne.id as itemOne.tobName for itemOne in itemsForAddrLevelOne"></select> 

也許你可以額外地改變NG-模型值到model並使用ng-options="itemOne as ..."(沒有.id)並添加track by itemOne.id

+0

謝謝你的回答! –

1
<select ng-model="yourModel" ng-options="itemOne.tobName for itemOne in itemsForAddrLevelOne"></select> 

在JS,你應該設置$scope.yourModel = itemsForAddrLevelOne[0];

+0

謝謝您的回答! –

0

我做了這樣......從AngularJS控制器我送與先前創建的對象的標識符的請求:

$http({ 
    url: '/api/get/object', 
    method: "POST", 
    data: {"id" : $scope.someId} 
}).success(function (data) {  
    $scope.object = angular.fromJson(data);  
}).error(function(errorData) { ... }); 

路線:

POST   /api/get/object    SomeController.object 

在播放動作控制器(我在服務器端使用Play Framework 1.3):

@Transactional 
public static void object() { 
    String id = request.params.get("body").split(":")[1]; 
    SomeObject someObject = SomeObject.findById(Long.parseLong(id));  
    renderJSON(someObject); 
} 

我分配標識呃到下拉列表。

<div data-ng-init="initComboBoxes()"> 
    <select id="livingComplexSelectId" 
      class="form-control m-b" 
      ... 

然後,從$scope.object我提取必要的ID,並通過它們填充選擇。

的AngularJS控制器的有關部分:

$scope.initComboBoxes = function() { 
    var livingComplexSelector = document.getElementById('livingComplexSelectId'); 
    livingComplexSelector.value = $scope.object.LivingComplexId; // set id 
    //Loading of dependent values...  
    // $timeout(function(){$scope.afterWaitInitComboBoxes()}, 5000); 
} 
相關問題