2014-12-04 95 views
0

我从服务器返回数组,并填充本地$ scope.customers(我已在调试器中对此进行了验证);不过,我似乎无法填充我选择标签与阵列:为什么选择标签不填充我的ng选项?

角控制器:

surchargeIndex.controller('SurchargeIndexController', [ "$scope", "customerService", "templateService", 
    function ($scope, customerService, templateService) { 
    customerService.getTest().then(function (customers) { $scope.customers = customers; }); 
    //templateService.getTemplates.then(function (templates) { $scope.getTemplates = templates; }); 
}]); 

CSHTML:

<div class="dropdown"> 
    <select ng-model="customerKey" ng-options="customer.Key as customer.Value for customer in customers"></select> 
    <button id="getTemplates" class="btn btn-primary" ng-click="getTemplates(customerKey)">Get Templates</button> 
</div> 

当我检查选择元素没有什么存在,但单:

<option value="?" selected="selected"></option> 

JSON字符串:

[ 
    { 
    Key: 500, 
    Value: "Test Customer 1" 
    }, 
    { 
    Key: 11304, 
    Value: "test Customer 2" 
    }, 
    { 
    Key: 11345, 
    Value: "Test Customer 3" 
    }, 
] 

服务:

surchargeIndex.service('customerService', [ 
    '$http', function ($http) { 
     this.getTest = function() { 
      return $http({ 
        method: "GET", 
        url: "api/Customer/GetTest", 
       }) 
       .success(function(data) { 
        return data; 
       }); 
     }; 
    } 
]); 

我用NG-reepat上的选项标签(我知道这是不是首选方式),我得到15(项目的数组中的数字)空白选项尝试。

我错过了什么?

+0

请问你的客户对象数组中有一个键和一个值的财产?上面应该工作,我把一个快速jsfiddle放在一起,它填充下拉列表中的数据:http://jsfiddle.net/5hk40xqx/ – Patrick 2014-12-04 16:38:17

+0

看起来很好,提供的代码。可能是因为你错过了完整实现中的某些东西。你可以包含'客户'的结构,或者可以在小提琴中重现问题吗? – kubuntu 2014-12-04 16:39:11

+0

@kubuntu新增json – Robert 2014-12-04 16:42:37

回答

2

传递给thensuccessCallback将以表示响应的对象的形式接收单个参数。

您要使用的data属性:

customerService.getTest().then(function(result) { 
    $scope.customers = result.data; 
}); 

或者您可以使用success方法来代替。传递给此回调接收四个参数:

success(function(data, status, headers, config) { 

例子:

customerService.getTest().success(function(data) { 
    $scope.customers = data; 
}); 
相关问题