Wondering Coder
Wondering Coder

Reputation: 1702

How to implement passing of variable from one service method to another angular

Hi to all angular guru,

My question is how to pass the return result of one service method to the other methods. Or to make it short I have an authentication method in my service, the return object result for this is a token. The token will be used to be append in my header for the rest of my http request which reside on the same service.

E.g

my Service js

authenticatePlayer: function(postData) {
    return $http({
      method  : 'POST',
      url     : api + 'auth/player',
      data    : postData,
      headers : {'Content-Type' : 'application/json'}
    })
    .then(function(result) {
      return result.data.token; //this is now the token
    }, function (result) {
      console.log(result);
    });
  }

within the Service js, I have other $http request such as:

        getPlayerByEmail: function(email_address) {
        return $http({
            method  : 'GET',
            url       : api + 'player/' + email_address,
            headers : {'X-token': token} 
           //token here is from the authenticatePlayer method but how to get it??
        })
        .then(function(result) {
            return result.data;
        });
    }

The two services methods are called in two controllers, my extended question is how to you pass the $scope from one controller to another that even when the page is refreshed the $scope value won't be destroy.

Hope it make sense.

Upvotes: 2

Views: 1634

Answers (2)

BKM
BKM

Reputation: 7079

One way to share $scope values between controllers is to create a service and inject it in any controller you want; An example service,

angular.module('myApp', [])
    .service('shareScope', function () {

        return {
            get: function () {
                return value;
            },
            set: function(data) {
                value = data;
            }
        };
    });

In your controller;

function Ctrl($scope, shareScope) {
    $scope.prop2 = shareScope.set('data');
    $scope.both = shareScope.get();
}

Upvotes: 4

JB Nizet
JB Nizet

Reputation: 691685

Store it in a variable:

angular.module('foo').factory('someService', function() {

    var token;

    return {
        authenticatePlayer: function(postData) {
            return $http({
                method  : 'POST',
                url     : api + 'auth/player',
                data    : postData,
                headers : {'Content-Type' : 'application/json'}
            })
            .then(function(result) {
                token = result.data.token; //this is now the token
            }, function (result) {
                console.log(result);
            });
        },
        getPlayerByEmail: function(email_address) {
            return $http({
                method  : 'GET',
                url       : api + 'player/' + email_address,
                headers : {'X-token': token} 
            })
            .then(function(result) {
                return result.data;
            });
        }
    };
});

Upvotes: 0

Related Questions