为什么选择标签没有填充我的 ng 选项

Why isn't the select tag populating my ng-options?

本文关键字:我的 ng 选项 填充 选择 标签 为什么      更新时间:2023-09-26

我从服务器返回了数组,并填充了本地$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>

当我检查选择元素时,除了一个元素之外什么都没有:

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

我错过了什么?

传递给

thensuccessCallback将接收一个表示响应的对象形式的参数。

要使用 data 属性:

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

或者,您可以改用success方法。传递给 this 的回调接收四个参数:

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

例:

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