3

I'm pretty new to with AngularJS. When I'm calling $http.get I get a $http is not defined error.

This is the content of my Module:

var demoApp = angular.module('demoApp', []);

demoApp.config(function ($routeProvider) {
    $routeProvider.
        when('/view1',
        {
            controller: 'SimpleController',
            templateUrl: 'View1.html'
        }).
        when('/view2',
        {
            controller: 'SimpleController',
            templateUrl: 'View2.html'
        })
        .otherwise({ redirectTo: '/view1' });
});

demoApp.factory('simpleFactory', function () {

    var factory = {};
    factory.getAnnounces = function ($http) {
        $http.post("http://localhost:57034/Announce/GetAllAnnounces")
           .success(function (data, status, headers, config) {
               return data;
           }).error(function (data, status, headers, config) {
               return status;
           });
           };
    return factory;
});

demoApp.controller('SimpleController', function ($scope,simpleFactory) {

    $scope.announces = [];
    init();
    function init()
    {
        $scope.announces= simpleFactory.getAnnounces();
    }

});

What am I missing here? Cheers.

1 Answer 1

10

You need to review your code as follows:

demoApp.factory('simpleFactory', ['$http', function ($http) {

    return {
        getAnnounces: function () {
            $http.post("http://localhost:57034/Announce/GetAllAnnounces")
               .success(function (data, status, headers, config) {
                   return data;
               }).error(function (data, status, headers, config) {
                   return status;
               });
        }
    };

}]);

There's no need to pass the $http variable in the getAnnounces method definition, because it is defined already in the scope of the factory function.

I am using parameter aliasing for AngularJS in order to avoid issues with minifiers, see 'A note on minification' on the AngularJS web site.

Note anyway that $http.post.success and $http.post.error are asynchronous and you won't be able to get the data unless you're using promises ($q), see here. Therefore you could change the code this way:

demoApp.factory('simpleFactory', ['$http', '$q', function ($http, $q) {

    return {
        getAnnounces: function () {
            var deferred = $q.defer();

            $http.post("http://localhost:57034/Announce/GetAllAnnounces")
               .success(function (data, status, headers, config) {
                   deferred.resolve(data);
               }).error(function (data, status, headers, config) {
                   deferred.reject(data);
               });

            return deferred.promise;
        }
    };

}]);

And in the SimpleController:

demoApp.controller('SimpleController', ['simpleFactory', '$scope', function (simpleFactory, $scope) {

    $scope.announces = []; 

    simpleFactory.getAnnounces()
        .then(function(data) {
            // call was successful
            $scope.announces = data;
        }, function(data) {
            // call returned an error
            $scope.announces = data;
        });

}]);
Sign up to request clarification or add additional context in comments.

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.