2017-09-07 1 views
0

私は配列サービスによってロードされた複数選択(select2 jquery)を持っているので、selectからいくつかのid値を取得し、空のモデルでjson配列をロードします。角度jsを使用して空のモデルでselectから配列をロードする方法は?

まず、残りのAPIから空のモデルを取得し、選択した値から内部配列をロードします。

私のサービスから取得したJSON空のモデル:

{ 
    "params": [{ 
      "id": 0, 
      "desc": "" 
}], 
    "person": { 
     "name": "", 
     "age": 0 
    } 
} 

私のコントローラJS:

app.controller('ctrl', ['$scope', function ($scope) { 

$scope.emptyModel = {}; 

$scope.initEmptyModel = function() { 
    $scope.emptyModel = getEmptyModelFromService(); //success 
} 

$scope.getObjectFromCombo = function(data){ 
    //if i select 3 options, i want load 3 objects in array 
    // but "data" contains a int Array and not object :/ 
    emptyModel.params.id = data.id; 
    emptyModel.params.desc = data.desc; 
} 


}]); 

私のhtmlを選択:

<div ng-init="initEmptyModel()"> 

    <input type="text" class="form-control" ng-model="emptyModel.person.name" /> <!--success load value--> 
    <input type="text" class="form-control" ng-model="emptyModel.person.age" /> <!--success load value--> 


    <select class="form-control" multiple="multiple" ng-model="aux" ng-change="getObjectFromCombo(aux)"> 
      <option ng-repeat="item in listService" value="{{item.id}}">{{item.desc}}</option> 
    </select> 

</div> 

答えて

0

はこれを試してみてください。

ng-repeatを使用する代わりに、ng-optionsディレクティブを使用してng-repeatを変更します。

<select class="form-control" multiple="multiple" ng-model="aux" ng-change="getObjectFromCombo()" ng-options="item.desc for item in listServicetrack by item.id"></select> 

そして、あなたのgetObjectFromCombo()関数で$scope.auxを使用。 $ scope.auxを配列として宣言してください。

$scope.getObjectFromCombo = function(){ 
    //print the content of the aux model 
    //this will print an array of object 
    console.log($scope.aux); } 
関連する問題