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 want to call .get in Angular factory. Can I please have any help why mydata is staying empty. I can see in console that json is loading but mydata variable has no value:

.factory('People', function($http) { $http.get("http://www.example.com/service_2.php").success(function(response) { var mydata = response;});

share|improve this question
    
What does console.log(response) give you? – camden_kid Mar 2 '15 at 20:16
    
Thanks for your help. Yes it logs the record but if i send JSON statically then controller shows right values and with above returned response as 'null'. – Sam222 Mar 2 '15 at 21:01
    
Controller is: .controller('MContactCtrl', function($scope, People) { $scope.values = alert(People.all()); }) and all function: return { all: function() { return mydata; – Sam222 Mar 2 '15 at 21:05

The code snippet is good but maybe you should try making mydata a global variable. that is, declare it outside the inner function and set it to null. So, upon the complete execution of the $http.get, echo mydata on a new line.

share|improve this answer
    
It shows JSON when i check in console but my controller stays empty and no results though if i put same JSON inside array statically it works. – Sam222 Mar 3 '15 at 14:01

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.