简体   繁体   中英

Angular Populate dropdown options dynamically while selecting dropdown in runtime

I have a JSON object containing names of dropdown and looks something like this -

$scope.obj = { 
    "dp1" :{},
    "dp2" :{}
}

Objects dp1 and dp2 correspond to respective dropdowns. These objects will be referred by dropdown to populate their options tag.

Next I have a REST call function like this -

$scope.getData = function(category, type) {
    var params = { "dp1" : category, "dp1__type": type};
    PromiseService.getPromiseData("GET", "/api/get_data/?" + $httpParamSerializer(params)).then(function(response) {
        $scope.obj.dp1= response;
    });
} 

I am able to assign the response to $scope.obj.dp1
The reponse object looks like this-

{ "id" : 1, "name" : "john" }


Finally, my dropdown looks like this -

<select id="d1" ng-model="d1">
    <option ng-repeat="opt in obj.dp1" t>{{opt.id}}_{{opt.name}}</option>
</select>

I want to populate the obj JSON object based on response, which I able to. Then I want to go to sub object in obj and apply ng-repeat on that to populate the options tag.

Current implementation of ng-repeat gives me undefined undefined .

try this-

$scope.obj = {

    "dropdown1" :{id:"dp2", data:"", options:[]},
    "dropdown2":{id:"dp1", data:"", options:[]}
}

$scope.getData = function(category, type){
           var params = { "dp1" : category, "dp1__type": type};
           PromiseService.getPromiseData("GET", "/api/get_data/?" + $httpParamSerializer(params)).then(function(response){
                    $scope.obj.dropdown1.options = response;
           });
       } 

html page -

<div ng-repeat="dropdown in obj">
   <select id="{{dropdown.id}}" ng-model="dropdown.data" ng-options="option.name for option in dropdown.options">
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM