2016-10-23 30 views
0

我正在使用服務爲本地JSON文件發出http請求。我試圖將成功的HTTP請求中的數據存儲在我的控制器中,但那部分不起作用。然而,http請求似乎在服務上是成功的。在服務上發出http請求,在控制器中獲取響應

var myModule = angular.module("MyApp", []) 

    .controller('MyController', function(Utilities){ 

    //this is not working 
    self.socialArr = Utilities.getData("data.json"); 

    }).service('Utilities', function($http) { 

    var self = this; 

    self.getData = function(jsonData) { 
     $http.get(jsonData) 
     .then(function(response) { 
      //First function handles success 
      var theData = response.data; 
      console.log(theData); //this works (data is logged) 
      return theData; 
     }, function(response) { 
      //Second function handles error 
      console.log("Error loading JSON data"); 
     }); 
    }; 
}); 

回答

4

不必返回從服務方法什麼。返回$http承諾並在控制器中使用then()將返回的數據分配給控制器屬性。另外你還沒有控制器進行定義self

angular.module("MyApp", []) 

.controller('MyController', function(Utilities) { 
    // store reference of "this" 
    var self = this; 
    // call service method 
    Utilities.getData("data.json").then(function(data) { 
    // assign data after promise resolves 
    self.socialArr = data; 
    }); 


}).service('Utilities', function($http) { 

    var self = this; 

    self.getData = function(jsonData) { 
    // return the promise 
    return $http.get(jsonData).then(function(response) { 
     return response.data; 
     }, function(response) { 
     //Second function handles error 
     console.log("Error loading JSON data"); 
     }); 
    } 
}); 
0

我想你應該使用承諾。因爲你在做一個異步調用。

self.getData = function(jsonData) { 
var deferred = $q.defer(); 
     $http.get(jsonData) 
     .then(function(response) { 

      if (response.data) { 
     deferred.resolve(response); 
    } else { 
     deferred.reject(response); 
    } 

     }, function(response) { 
      deferred.reject(response); 
     }); 
return deferred.promise; 
    }; 

然後在控制器

Utilities.getData("data.json").then(function(res) 
{ 
    self.socialArr=res; 
},function(e){ 

}); 
+2

使用'$ q'是[反模式](http://stackoverflow.com/questions/23803743/什麼是顯式承諾建設反模式和如何做我避免它)當$ http'已經返回一個承諾 – charlietfl

+0

沒有我創建一個自定義承諾給http返回承諾這種方式它會工作。 – saiyan

+0

不,我創建一個自定義的承諾,以http的方式返回承諾這種方式它將工作。對於使用函數來進行http調用的方法,上面的答案可以工作或使用其承諾inline.see http調用http調用http:// chariotsolutions。 com/blog/post/angularjs-corner-using-promises-q-handle-asynchronous-calls/**關於後期處理**部分怎麼樣 – saiyan

0

您應該返回)的$ http.get(後一個承諾,然後(successCallback)。在你的代碼

return語句,

return theData; 

只限於和 「作用域」 到了successCallback。因此,爲了獲得它的句柄,您需要返回與successCallback相關的承諾。

  self.getData = function(jsonData) { 

     // store the promise 
     var promise = $http.get(jsonData) 
     .then(function(response) { 
      //First function handles success 
      var theData = response.data; 
      console.log(theData); //this works (data is logged) 
      return theData; 
     }); 

     // return the promise, which can get you the successCallback's returned data. 
     return promise; 
     };   

console.log輸出表示代碼已執行,但返回的數據在沒有與其關聯的承諾時無效。

在控制器:

  Utilities.getData("data.json").then(function(socialArr){ 
      $scope.socialArr = socialArr; 
      // this should print 
      console.log($scope.socialArr); 
      }); 

一個工作plunkr here

0
var myModule = angular.module("MyApp", []) 

.controller('MyController', function(Utilities){ 

    Utilities.getData("data.json").success(function(responce) { 
     self.socialArr = response.data; 
    }) 

}) 
.service('Utilities', function($http) { 

    var self = this; 

    self.getData = function(jsonData) { 
     return $http.get(jsonData).error(function(responce) { 
      console.log("error!"); 
     }) 
    }; 
}); 
相關問題