javascript - How do I return data from a $http.get() inside a factory in angularjs -


i'm having lot of trouble , can't seem find here on or google helps me spot i'm doing wrong

<!doctype html> <html data-ng-app="testapp">    <head>       <title></title>    </head>    <body>       <div data-ng-controller="mycontroller">          {{test}}<br/>          {{test2}}<br/>          {{test3}}          <ul>             <li data-ng-repeat="member in members">{{ member.firstname}}</li>          </ul>       </div>       <script type="text/javascript" src="angular.min.js"></script>       <script type="text/javascript">          angular.module('testapp', ['memberfactory']);           angular.module('testapp',[])          .factory('memberfactory', function($http){              var obj = {};             obj.data = "abcd";             obj.getresponse = function(){                 var temp = {};                 $http.get('hello.php').success(function(data){                         alert(data);                         temp =data;                  });                  return "some return value";             }              return obj          });           function mycontroller($scope, memberfactory){              $scope.test= "teststring";             $scope.test2= memberfactory.data;             $scope.test3= memberfactory.getresponse();           }       </script>    </body> </html> 

the return "some return value"; works when try return temp, null. have tried various ways work can't seem set temp value inside $http.get() function

it simple (or silly mistake/misguided approach on part). advice appreciated

use defer:

obj.getresponse = function(){                      var temp = {};     var defer = $q.defer();     $http.get('hello.php').success(function(data){             alert(data);             temp =data;             defer.resolve(data);      });     return defer.promise; } 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -