1

I'm trying to display a list of events after ajax callback using AngularJS

HTML:

<ul>
    <li ng-repeat="event in events">{{ event.content }}</li>
</ul>

CONTROLLER:

app.controller("myctrl", function ($scope, mySrv) {
    $scope.message = "";
    $scope.events = mySrv.getEvents(26);

});

SERVICE:

app.factory('mySrv', function ($http) {
    return {
        getEvents: function (i_RequestID) {

            $http({
                method: 'POST',
                url: '/Home/GetOptimizationResultEvents',
                data: { RequestID: i_RequestID }
            }).then(function successCallback(response) {
                return response.data;
            }, function errorCallback(response) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
            });
        }
    }
});

Now there are no errors but i see no results.. what am i dooing wrong here ?

2
  • Are you using angular with webAPI? Commented Dec 8, 2015 at 12:35
  • 3
    you have to return the $http Commented Dec 8, 2015 at 12:35

4 Answers 4

2

just like @koox00 said, you have to return the $http

app.factory('mySrv', function ($http) {
    return {
        getEvents: function (i_RequestID) {

            $http({
                method: 'POST',
                url: '/Home/GetOptimizationResultEvents',
                data: { RequestID: i_RequestID }
            });
        }
    }
});

Personally I prefer to transform my raw data in the factory

app.factory('mySrv', ['$q', '$http', function ($q, $http) {
    return {
        getEvents: function (i_RequestID) {
            return $q(function(resolve, reject) {
                $http({
                    method: 'POST',
                    url: '/Home/GetOptimizationResultEvents',
                    data: { RequestID: i_RequestID }
                })
                .then(function(response) {
                    var data = response.data;
                    // transforms

                    resolve(data);
                })
                .catch(function(reason) {
                    reject(reason);
                });
            });
        }
    }
}]);
Sign up to request clarification or add additional context in comments.

Comments

0

your getEvents-functions needs to return the promise of the call:

getEvents: function (i_RequestID) {

        return $http({
            method: 'POST',
            url: '/Home/GetOptimizationResultEvents',
            data: { RequestID: i_RequestID }
        }).then(function successCallback(response) {
            return response.data;
        }, function errorCallback(response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
    }

This will make getEvents return a promise, so where you use it you go like this:

app.controller("myctrl", function ($scope, mySrv) {
    $scope.message = "";

    mySrv.getEvents(26).then(function(result){
        $scope.events = result;
    });

});

you might need to do a $scope.$apply() after you assign $scope.events = result;

Comments

0

Just a stab in the dark, but have you tried returning the promise?

app.factory('mySrv', function ($http) {
    return {
        getEvents: function (i_RequestID) {

        return $http({
                method: 'POST',
                url: '/Home/GetOptimizationResultEvents',
                data: { RequestID: i_RequestID }
            }).then(function successCallback(response) {
                return response.data;
            }, function errorCallback(response) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
            });
        }
    }
});

Comments

-1

try this

     app.controller("myctrl",['$scope', 'mySrv', function ($scope, mySrv) {
           $scope.message = "";
           $scope.events = mySrv.getEvents(26);

    }]);

also check with controller and app script files properly loaded

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.