2

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.

1
  • As far as I know, your AngularJS model is destroyed when moving from one page to another. You would have to store the token in a cookie or other local storage if you want to retain it between pages. Commented Aug 22, 2013 at 9:15

2 Answers 2

4

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Why use a global window variable when the variable could be limited to the scope of the service?
but when the user refreshes the page the variable will be reset right/
In JavaScript if you refresh the page the variable scope will be lost. If you want the variable scope even after refreshing, you should use cookies, localStorage or sessionStorage.
0

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

2 Comments

the problem with the above solution is when the user refresh the page the variable reset. How to avoid that?
Any JavaScript state, will always disappear if you refresh the page. If the state must be kept for several pages, you must store it in some persistent location: a cookie (see $cookieXxx in the angular doc), localStorage, on on the server.

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.