Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am trying to create a service to get json and pass it to me homeCtrl I can get the data but when a pass it to my homeCtrl it always returns undefined. Im stuck.

MY SERVICE
var myService = angular.module("xo").factory("myService", ['$http', function($http){
return{
 getResponders: (function(response){

     $http.get('myUrl')
             .then(function(response){
         console.log("coming from servicejs", response.data);
     });
 })()
};
return myService;
}
]);



MY HOME CTRL

var homeCtrl = angular.module("xo").controller("homeCtrl", ["$rootScope", "$scope", "$http", "myService",
function ($rootScope, $scope, $http, myService) {
 $scope.goData = function(){
     $scope.gotData = myService.getResponders;
 };
 console.log("my service is running", $scope.goData, myService);
}]);
share|improve this question

You should return promise from getResponders function, & when it gets resolved it should return response.data from that function.

Factory

var myService = angular.module("xo").factory("myService", ['$http', function($http) {
    return {
        getResponders: (function(response) {

            return $http.get('myUrl')
            .then(function(response) {
              console.log("coming from servicejs", response.data);
              //return data when promise resolved
              //that would help you to continue promise chain.
              return response.data;
            });
        })()
    };
    return myService;
}]);

Also inside your controller you should call the factory function and use .then function to get call it when the getResponders service function resolves the $http.get call and assign the data to $scope.gotData

Code

 $scope.goData = function(){
     myService.getResponders.then(function(data){
          $scope.gotData = data;
     });

 };
share|improve this answer
    
What if I wanted $scope.goData to return data? Because if I init another variable inside $scope.goData and then make it equal to data or $scope.gotData, it would have been returned before it gets the valye – Karan Kapoor Dec 27 '15 at 18:57

This is an example how I did for my project, it work fine for me

var biblionum = angular.module('biblioApp', []);//your app
biblionum.service('CategorieService', function($http) {


    this.getAll = function() {

        return $http({
            method: 'GET',
            url: 'ouvrage?action=getcategorie',
            // pass in data as strings
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}  // set the headers so angular passing info as form data (not request payload)
        })
                .then(function(data) {

                    return data;


                })


    }


});

biblionum.controller('libraryController', function($scope,CategorieService) {
  
    var cat = CategorieService.getAll();
    cat.then(function(data) {
        $scope.categories = data.data;//don't forgive this
    })

  });

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.