为什么选择标签不会填充我的ng-options?

时间:2014-12-04 16:30:36

标签: javascript angularjs

我从服务器返回数组并填充了本地$ scope.customers(我在调试器中验证了这一点);但是,我似乎无法使用数组填充我的选择标记:

角度控制器:

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>

当我检查select元素时,除了单个元素之外什么都没有:

 <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(数组中的项目数)空白选项。

我错过了什么?

1 个答案:

答案 0 :(得分:2)

传递给successCallback的{​​{1}}将以表示响应的对象形式接收单个参数。

您想使用then属性:

data

或者,您可以使用customerService.getTest().then(function(result) { $scope.customers = result.data; }); 方法。传递给它的回调接收四个参数:

success

示例:

success(function(data, status, headers, config) {
相关问题