Shubham Rai
Shubham Rai

Reputation: 39

I got a data(token from server) on one controller, and want to use that data(token) in another controller in Angular js?

i am getting data from server in form of token in controller named "adminSearchCtrl" and want to use that token to another controller named "adminViewCtrl" how to do that??

adminSearchCtrl.js

$scope.getUserDetails = function(selectedUser){
        console.log(selectedUser, userToken.token);
        AdminSearchService.getUserDetails(selectedUser, userToken.token)
        .then(function(response){
            $scope.switchUserToken = response.switchToken.token;
        })
        .catch(function(err){
            $scope.error = err.message;
        });
    }

i want to use this token "$scope.switchUserToken" to another controller named "adminViewCtrl" I have put to $scope also but when i am using "$scope.switchUserToken" to my "adminViewCtrl" it is giving undefined can any body help me out??

Upvotes: 0

Views: 84

Answers (1)

jjimenez
jjimenez

Reputation: 893

Each controller has its own $scope, you can't access them from one to another, and you shouldn't.

The solution is to create a service to share this data between controllers. Here you have a JSBin with an example.

Upvotes: 1

Related Questions