2014-10-08 44 views
0

我有點新角,我想從我寫的web服務中獲取一些數據。我發送一些變量作爲帖子。但是我怎樣才能得到函數中的http變量。它可能是一個愚蠢的問題,但我找不到解決方案。我需要它在一個函數中,因爲我想多次調用它。Angularjs:在函數中獲取http變量

kljControllers.controller('CalendarPageController', ['$scope', 
     function($scope, $http) { 
      $scope.GetEvents = function() { 
       var dataToPost = Array(); 
       dataToPost.push($scope.month); 
       dataToPost.push($scope.year); 

       $scope.http.post('http://localhost:8080/webservice/calendarevent'). 
       success(function(data, status, headers, config) { 
        $scope.events = data.events; 
       }). 
       error(function(data, status, headers, config) { 
        document.write("status"); 
       }); 
      } 
     } 
} 

回答

1

您只需使用$http - 無需用$scope

http.post('http://localhost:8080/webservice/calendarevent'). 
     success(function(data, status, headers, config) { 
      $scope.events = data.events; 
     }). 
     error(function(data, status, headers, config) { 
      document.write("status"); 
     }); 

到前言它你也缺少它在你的控制器聲明:

kljControllers.controller('CalendarPageController', ['$scope', "$http", 
                    ^^^^^ 
1

要麼

kljControllers.controller('CalendarPageController', ['$scope', '$http', function($scope, $http) { 

或只是通過功能,不依賴陣列

kljControllers.controller('CalendarPageController', function($scope, $http) { 

現在你可以使用$ HTTP這樣的:

$http.get("some url").then(function(result) { 
    console.log(result); 
}, function(err) { 
    console.log(err); 
});