0

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;});

3
  • What does console.log(response) give you? Commented Mar 2, 2015 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'. Commented Mar 2, 2015 at 21:01
  • Controller is: .controller('MContactCtrl', function($scope, People) { $scope.values = alert(People.all()); }) and all function: return { all: function() { return mydata; Commented Mar 2, 2015 at 21:05

1 Answer 1

0

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.

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

1 Comment

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.

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.