0

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

2 Answers 2

1

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", 
                                                                  ^^^^^
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.