swati kiran swati kiran - 7 months ago 33
Javascript Question

select key/value in angularjs

Im getting json values from http service for each id.. trying to give a drop down with list of names and ids as key to store . If I have this in my html




<select>
<option ng-repeat="(key, value) in data" value="{{key}}">{{value}}</option>
</select>



this in my controller




$http.get('/api/v1/secure/admin/groups/v/users').success(function(response) {
var j=[];
var k=[];

$scope.data=[];

$scope.vId=[];
$scope.vName=[];

console.log(response[0]._id)//getting 1st id
console.log(response[0].name.prefix+" "+response[0].name.first+" "+response[0].name.last)//getting name

for (var i =0; i<response.length; i++) {

k=(response[i]._id);
j= (response[i].name.prefix+" "+response[i].name.first+" "+response[i].name.last);

console.log("list of visit manager: "+$scope.vManagerName+" id "+$scope.vManagerId);

};

$scope.data={
$scope.vManagerId.push(k) : $scope.vManagerName.push(j)
}

});


but its undefined ...please help..thanks in advance

Answer

You don't need to map on controller as you are doing wrong

Try like this

View

<select ng-model="model" ng-options="d._id as d.name.prefix+' '+d.name.first+' '+d.name.last for d in data">   
</select>

CTRL

$scope.data=response

DEMO