2016-07-04 20 views
0

で解決された後intead配列のオブジェクトの配列を与える私は私のHTTPサービス(UserServiceの)中に次のコードを持っている:角度の約束は、コントローラ

module1.factory('UserService', ['$http', '$q', function($http, $q) { 



     return { 

     getModelValueTableValues1: function(CarLine, GeographyType, GeographyName, Duration, ModelId, ScenarioId, TimeKey) { 



      return $http.get('http://localhost:8080/scenarioplan/models/data?ScenarioId=5&CarLine=Nissan%20-%20Rogue&TimeKey=Jul-16&Duration=1&Geographytype=National&GeographyName=National&ModelId=1001') 
      .then(
       function successCallback(response) { 



       return response.data; 


       }, 
       function errorCallback(errResponse) { 
       console.error('Error while fetching Model Parameter Table'); 
       return $q.reject(errResponse); 
       } 
      ); 
     }, 

をし、私は自分のコントローラに次のコードを持っている:

$scope.getModelValueTableValues = function(CarLine, Geography, Duration, ModelId, ScenarioId) { 

    UserService.getModelValueTableValues1(CarLine, Geography.split('-')[0], Geography.split('-')[1], Duration, ModelId, ScenarioId, '16-Jul') 
    .then(
     function(d) { 

     console.log("the data object from promise is", d); 
     console.log("the data object from promise is", d.length); 

     console.log('The Promise has been successfully resolved!!!!'); 
     }, 
     function(errResponse) { 
     console.error('The Promise was not successfull'); 
     } 
    ); 
}; 

データの配列を返す代わりに、プロミスは解決された後、デバッグウィンドウ(F12)の下の画像に示すようにオブジェクトの配列を返します。 enter image description here

以下F12コンソール出力が示すように、Webサービスの呼び出しからの応答に有効なデータに思える:

enter image description here

誰かがこの問題を解決するために私を助けてくださいことはできますか? 私は一日を通してこれを解決しようとしていますが、成功することはありません!

+0

あなたの '応答 'には何がありますか? http:// localhost:8080/scenarioplan/models/data?ScenarioId = 5&CarLine = Nissan%20-%20ログ&TimeKey = 7月16日と期間= 1&Geographytype = National&GeographyName = National&ModelId = 1001'これは返信しますか? –

+0

こんにちは@SusheelSingh、私質問に画像を添付しました。 – Sabyasachi

+1

'データの配列'はもう少し説明できますか? –

答えて

1

$ httpは既に約束しています。あなたの関数は、

return $http.get('http://localhost:8080/scenarioplan/models/data?ScenarioId=5&CarLine=Nissan%20-%20Rogue&TimeKey=Jul-16&Duration=1&Geographytype=National&GeographyName=National&ModelId=1001'); 

のいずれかにする必要があります。機能の中にプロセスデータを入れたい場合は、そうする必要があります。

return { 

    getModelValueTableValues1:function(CarLine,GeographyType,GeographyName,Duration,ModelId, ScenarioId,TimeKey){   
    var deffered = $q.defer(); 
    $http.get('http://localhost:8080/scenarioplan/models/data?ScenarioId=5&CarLine=Nissan%20-%20Rogue&TimeKey=Jul-16&Duration=1&Geographytype=National&GeographyName=National&ModelId=1001') 
     .then(
        function successCallback(response){ 
            deffered.resolve(response); 
           }, 
        function errorCallback(errResponse){ 
          console.error('Error while fetching Model Parameter Table'); 
          deffered(errResponse); 
          } 
         ); 
     return deffered.promise; 
     }, 
+0

@vanmohit ...あなたのアプローチは、私に道を部分的に示しています。私はあなたにこれを試してみてください。ありがとう! – Sabyasachi

+0

こんにちは@Vanmohit .....私は最終的にそれを働かせることができました。あなたの関数から返されたデータを処理するために、私のコントローラの中で関数JSON.parse(angular.toJson(d.data))を使用しなければなりませんでした。 – Sabyasachi

+0

'.then'の中でデータを処理したい場合でも' $ http'の約束を返すことができます。関数内で結果を返すだけです。リジェクト側で 'return $ q.reject(reason)'を使用します。 – Icycool

0

ここでは、私の問題を解決するために従った完全な手順を示します。

  1. @vanmohitによって示唆されるように:、私のコントローラのコードで

    return { 
        getModelValueTableValues1: function(CarLine,GeographyType,GeographyName,Duration,ModelId, ScenarioId,TimeKey){   
         var deffered = $q.defer(); 
         $http.get('my url here') 
          .then(
            function successCallback(response){ 
             deffered.resolve(response); 
            }, 
            function errorCallback(errResponse){ 
             console.error('Error while fetching Model Parameter Table'); 
             deffered(errResponse); 
            } 
          ); 
         return deffered.promise; 
        }, 
    
  2. 私は、この機能を使用: 私はこのような私のappfactoryサービス内の彼のコードを使用

    function(CarLine, Geography,Duration,ModelId, ScenarioId){ 
        UserService.getModelValueTableValues1(CarLine,Geography.split('-')[0],Geography.split('-')[1],Duration,ModelId,ScenarioId,'16-Jul') 
         .then(
          function(d) { 
           console.log("the data object from promise is",JSON.parse(angular.toJson(d.data))[1].paramName); 
           //console.log("the data object from promise is",d.length); 
           //JSON.parse(angular.toJson(resp)) 
           $scope.firstrow = JSON.parse(angular.toJson(d.data)); 
           console.log('The Promise has been successfully resolved!!!!'); 
          }, 
          function(errResponse){ 
           console.error('The Promise was not resolved'); 
          } 
         ); 
        }; 
    

これでコントローラのJSONデータを解析できました。 ありがとうございました!

+0

十分な$ scope.firstrow = d.dataを使用することができます。デフォルトで$ httpはJSONにデータを解析します。 – vanhonit

+0

@vanhonit ....また、私は約束から返された値を使用して変数に保存することができません...非同期呼び出しであるため、スコープ変数に保存することができません。それを手伝ってもらえますか? – Sabyasachi

+0

@vanmohit $ scopeにアクセスできません。$ scope.firstrow = d.dataと書いた後、この関数の外部にあるfirstrow変数。私は関数の外で$ scope.firstrow変数にアクセスしました。これは未定義として返されます。なぜそうなのか? – Sabyasachi

関連する問題