簡體   English   中英

用ajax和angular填充選擇選項

[英]filling select option with ajax and angular

我有一個選擇框,我想填充從ajax文件中獲得的數據,這是我擁有的代碼,但是我的選擇下拉列表中沒有任何數據。

html:
    <select ng-model="selectedTestAccount" ng-options="item.Id as item.Name for item in testAccounts">
        <option value="">Profiles</option>
    </select>

js:
    angular.module('test', []).controller('DemoCtrl', function ($scope, $http) {
$scope.selectedTestAccount = null;
$scope.testAccounts = [];

$http({
        method: 'GET',
        url: 'http://duda-api-test.herokuapp.com/profiles',
        data: { applicationId: 3 }
    }).success(function (result) {
    $scope.testAccounts = result;
});

});

您是否考慮使用指令?

.directive('mySelect', ['$scope','$http', function ($scope, $http) {
return {
    restrict: 'AE',
    replace: true,
    transclude: false,
    scope: { ngModel: '@'},      
    template: '<select "' +
                'ng-options="item.Id as item.Name for item in List">' +
                '<option value="">Profiles</option> ' +
              '</select>',
    link: function ($scope) {

    $http({
            method: 'GET',
            url: 'http://duda-api-test.herokuapp.com/profiles',
            data: { applicationId: 3 }
        }).success(function (result) {
        $scope.List = result;
    }
};
}]);

html

<my-select ng-model="selectedTestAccount></my-select>

在查看數據源時:

http://duda-api-test.herokuapp.com/profiles

您的ng-options錯誤。 它應該是:

ng-options="item.id as item.full for item in testAccounts"

您會獲得ID和完整的ID,而不是ID和名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM