0

I'm trying to run a function inside .factory to get posts, but I get an undefined error on my controller.

.factory('Portfolio', function($http, $log) {
  //get jsonp
    this.getPosts = function($scope) {
        $http.jsonp("http://test.uxco.co/json-test.php?callback=JSON_CALLBACK")
            .success(function(result) {
                $scope.posts = $scope.posts.concat(result);
                $scope.$broadcast("scroll.infiniteScrollComplete");
            });
    };
});


.controller('PortfolioCtrl', function($scope, Portfolio) {
    $scope.posts = [];
    Portfolio.getPosts($scope);

Thanks!

1
  • Fixed - having a doh moment! used .service instead Commented Aug 12, 2014 at 9:41

2 Answers 2

1

What you wrote is a service not a factory. so you either

  • change it to be .service instead of .factory.
  • or return the function inside an object like this
.factory('Portfolio', function($http, $log) {
    //get jsonp
    return {
        getPosts: function($scope) {
            $http.jsonp()
                .success(function(result) {
                    $scope.posts = $scope.posts.concat(result);
                    $scope.$broadcast("scroll.infiniteScrollComplete");
                });
        };
    }

});

check the difference between service and factory

Sign up to request clarification or add additional context in comments.

Comments

0

Use .factory code like this

.factory('Portfolio', function($http, $log) {
  //get jsonp
   return{
       getPosts: function($scope) {
        $http.jsonp("http://test.uxco.co/json-test.php?callback=JSON_CALLBACK")
        .success(function(result) {
            $scope.posts = $scope.posts.concat(result);
            $scope.$broadcast("scroll.infiniteScrollComplete");
        });
      };
    }
});

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.