0

下面是我的AirTableService.js

(function () {
    "use strict";   
    var AirTableService = function ($http, $q) {
        var AirTableMethods = {
            getMyRounds: function(AirTable_secret){
                var deferObject_myRounds;
                var myRounds_promise = $http.get('https://api.airtable.com/v0/XXXXXXX/Rounds?view=Main%20View&maxRecords=10&callback=JSON_CALLBACK', {
                    headers : {
                        'Authorization' : AirTable_secret.apikey,
                        'Content-Type' : 'application/json'
                    }
                });
                deferObject_myRounds = deferObject_myRounds || $q.defer();

                myRounds_promise.then(function(data){
                    deferObject_myRounds.resolve(data);
                });                
                return deferObject_myRounds.promise;
            }
        };
        return AirTableMethods;
    };

    AirTableService.$inject = ['$http', '$q'];

    angular.module('appGolf')
      .service('AirTableService', AirTableService);

}());

如您所见,使用AirTableapi正在尝试GET从我的表中获取数据。我正在传递参数view并且maxRecords它有效。文件说明我可以通过sort

在此处输入图像描述

然后我改为,

https://api.airtable.com/v0/XXXXXXX/Rounds?view=Main%20View&maxRecords=10&sort=[{field:'RoundID',direction:'desc'}]&callback=JSON_CALLBACK

很明显,这不起作用,它给了我这个错误, 我知道这是因为是 a并且我知道我如何传递这是不正确的。在此处输入图像描述
sortarray of objects

我的问题是,你如何做到这一点AngularJS

先感谢您。

4

2 回答 2

2

在这里找到答案 正如那里提到的,我需要添加,

paramSerializer: '$httpParamSerializerJQLike',

如果你有兴趣,我的功能现在看起来像,

var myRounds_promise = $http.get('https://api.airtable.com/v0/XXXXX/Rounds?callback=JSON_CALLBACK', {
                params: {
                    view: 'Main View',       
                    maxRecords: 10,
                    sort: [{"field": 'RoundID', "direction":'desc'}]                        
                },
                paramSerializer: '$httpParamSerializerJQLike',                    
                headers : {
                    'Authorization' : AirTable_secret.apikey,
                    'Content-Type' : 'application/json'
                }
            });

感谢大家的建议和帮助我。

于 2016-05-03T18:12:18.510 回答
1

您的服务非常冗长且难以阅读。我会这样写:

var app = angular.module("myApp", [ /* dependencies */ ]);

app.factory("AirTableService", ["$http", function($http) {
    return {
        getMyRounds: function(AirTable_secret) {
            return $http.get('path/to/API', {
                //put your sorting JSON object here in params
                params: { sort: [{field: "RoundID", direction: "desc"}] },
                headers: {
                    'Authorization' : AirTable_secret.apikey,
                    'Content-Type' : 'application/json'
                }
            });
        },
        anotherMethod: function() {
            //etc....
        },
        yetAnotherMethod: function() {
            return $http.post(); //maybe POST something
        }
    };
}]);

将其注入您的控制器并使用:

AirTableService.getMyRounds(airtableSecret).then(successCallback).catch(errorCallback);
于 2016-05-03T14:30:20.190 回答