0

I have this piece of code:

$scope.GetEmp= function () {
    $http.get("/api/emp/GetEmps")
        .success(function (data) {
            $scope.Emps= data;
            hideLoader();
        })
        .error(function (error) {
            $scope.operation_result = { result: false, Message: 'Unable to complete the action due to API problem' };
            $scope.show_result = true;
            hideLoader();
        });
};

That Method return this: [["1","Elmer Chacon"],["2","Juan Perez"],["3","Mauricio Lopez"]]

How can I populate a dropdown with this kind of data?

Thanks in advance.

2 Answers 2

3

You could do it like this:

Html

<div ng-controller="MyCtrl">
  <select ng-model="selected">
    <option ng-repeat="item in data" value="{{item[0]}}" >{{item[1]}}</option>
  </select>
  <div>Selected: {{selected}}</div>
</div>

Angular controller:

var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
    $scope.data = [["1","Elmer Chacon"],["2","Juan Perez"],["3","Mauricio Lopez"]];
}

You should load your data using your method, ofcourse.

Fiddle: https://jsfiddle.net/jorisheus/69jv0ykm/1/

Sign up to request clarification or add additional context in comments.

Comments

1

Similar to Joris's answer but using ng-options you can do something like this:

HTML:

<div ng-controller="MyCtrl">
  <select ng-model="selected" ng-options="item[0] as item[1] for item in data"></select>
  <div>Selected: {{selected}}</div>
</div>

Angular controller:

var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
    $scope.data = [["1","Elmer Chacon"],["2","Juan Perez"],["3","Mauricio Lopez"]];
}

Fiddle: https://jsfiddle.net/69jv0ykm/2/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.