使用数组填充DropDown - AngularJS和C#

时间:2017-05-01 15:51:17

标签: c# html angularjs

我有这段代码:

$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();
        });
};

该方法返回此: [[" 1"," Elmer Chacon"],[" 2"," Juan Perez"],[" 3&# 34;," Mauricio Lopez"]]

如何使用此类数据填充下拉列表?

提前致谢。

2 个答案:

答案 0 :(得分:3)

你可以这样做:

<强> 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>

角度控制器:

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

您应该使用当前的方法加载数据。

<强>小提琴: https://jsfiddle.net/jorisheus/69jv0ykm/1/

答案 1 :(得分:1)

与Joris的答案类似,但使用ng-options可以做到这样的事情:

<强> 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>

角度控制器:

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

小提琴: https://jsfiddle.net/69jv0ykm/2/