2014-10-08 69 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); 
});