2016-05-03 10 views
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); 

}()); 

です。私はパラメータviewmaxRecordsを渡しています。私はそれは私が enter image description here
、それは私に、このエラーを与えるには動作しません

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

と明確に変更し、私はsortを渡すことができ ドキュメント状態、

enter image description here

sortarray of objectsなので、私はこれをどのように渡しているのか分かりません。

私の質問は、どうすればAngularJSでですか?

ありがとうございます。

+0

[AngularJS '$ httpサービス]を使用して[GETリクエストで配列を送信](http://stackoverflow.com/questions/19957858/send-array-via-get-request-with-angularjs-http-service)の可能な複製 –

+0

例のように二重引用符を試してみましたか? – DrewJordan

+0

'$ http'はすでに約束を返しています。 '$ q'を使う必要はありません。 – Kyle

答えて

2

は答えを見つけることがhere が述べたように、私は追加する必要が、

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' 
       } 
      }); 

皆様のおかげで、私を助けてくれてありがとうございます。

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); 
+0

私はあなたが示唆したようにしようとしましたが、それでも同じエラーが発生しました。 BTWは私のサービスのフォーマットに感謝します。 – MadushM