kevingoos
kevingoos

Reputation: 4313

Angularjs: getting http variable in function

I am kinda new to angular and I want to get some data from a webservice I wrote. I am sending some variable with it as a post. But how can I get the http variable in the function. It can maybe be a stupid question, but I cannot find the soluction. I need it in a function because I want to call it a couple of times.

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

Upvotes: 0

Views: 43

Answers (2)

Zenorbi
Zenorbi

Reputation: 2644

either

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

or just pass the function without the dependency array

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

Now you can use the $http like this:

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

Upvotes: 1

tymeJV
tymeJV

Reputation: 104785

You simply use $http - no need to preface it with $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");
        });

You are also missing it in your controller declaration:

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

Upvotes: 1

Related Questions