1

I just tried to create a service in Angular JS , it succeeded call the function $http.get and return the value. However the value isnot inserted into the scope variable. Question : what is wrong?

 angular.module('starter.controllers', [])
.factory('UserService', function($http) {
var data;   
      return{
          getData: function($http) {
              return  $http.get('http://www.website.com/call12').
                    success(function(response) {
                     /// console.log(JSON.stringify(response));
                      userData=response.data;
                            return userData;

                   }).error(function(data, status, headers, config) {
                     // log error
                    });

          }
    }
 })


 .controller('AppCtrl', function($scope, $ionicModal,    $interval,$http,$rootScope,UserService) {

$scope.formData={};
 $scope.userData={};

$scope.myCtrl= function(UserService,$http,$rootScope) {

  UserService.getData($http).then(function(data,$rootScope) {
$scope.userData = data;

 $scope.fullName =  data.name;  
 $scope.balance =  data.balance;

   }}

  $scope.formData.playerName= $scope.userData.name; // $scope.userData is undefined

 $scope.myCtrl(UserService,$http);
 })

Templates

<form ng-submit="submit_editpw()" ng-controller="AppCtrl">  
<pre>userData : {{userData}}</pre> //retuns data
<pre>name: {{fullName}}</pre> //returns empty
</form>

2 Answers 2

2

You are not using asynchronous getData method right. Since this is a promise object you should use its then method:

UserService.getData().then(function(data) {
    $scope.userData = data;
});

Also make sure getData returns a promise:

getData: function () {
    return $http.get('http://m.sepakbola.cc/index.php/id/user/call12').success(function (response) {
        userData = response.data;
        return userData;
    }).error(function (data, status, headers, config) {
        // log error
    });
}
Sign up to request clarification or add additional context in comments.

5 Comments

hi . I got error. TypeError: UserService.getData(...) is undefined script: UserService.getData().then(function(data) { $scope.userData = data; });
Make sure getData returns promise: return $http.get(...).
Hi @dfsq I got issue when I try to get the service variable value. can you help me? for example ` $scope.fullName = data.name; ` ` // fullName is still empty` outside the function ` $scope.formData.playerName= $scope.userData.name; ``// $scope.userData is undefined`
Note, that you can't access data until it's loaded. So if you are trying to do in outside then it might be not yet available.
it has finished loading the http request and the value of userData.data.name can be viewed in template but fullName in template is still empty. and what about this $scope.formData.playerName= $scope.userData.name; // $scope.userData is undefined `
0

In the service return the promise as response and in the controller using the promise success callback assign the value to the userData

 angular.module('starter.controllers', [])
.factory('UserService', function($http) {
var data;   
      return{
          getData: function() {
                  return $http.get('http://www.website123.com/call12');
          }
    }
 })


 .controller('AppCtrl', function($scope, $ionicModal, interval,$http,$rootScope,UserService) {
      $scope.myCtrl= function(UserService,$http) {
      $scope.userData={};

      UserService.getData().success(function(reponse) {
          $scope.userData=response.data;
      });

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.