0

I want to share data from controller 1 to another controller

I get an undefined error in my 2nd controller. Is anyone able to tell me whats wrong with my code?

Service

app.service('var_transfer_service', function(){
       var test_var;              

          return {
             getVar: function () {
                return test_var;
           },
             setVar: function( _test_var ) {
                test_var = _test_var; 
                console.log(test_var);//show the object content in my console
            }
        }
   })

Controller 1

app.controller('homeCtrl', function($scope,$http, $filter ,var_transfer_service){
$scope.search_item = function ($event,item){
         console.log(item)
         var latt = item.lat;
         var lngt = item.lng;

         var_transfer_service.setVar(item); 
     }       

});

Controller 2

 app.controller('MapCtrl',function($scope,var_transfer_service, $state, $cordovaGeolocation) {
    var transferred_var = var_transfer_service.getVar();
    console.log(transferred_var); //undefined object
 });
1
  • 1
    Looks like your controller MapCtrl execute before homeCtrl. Show your corresponding html code, where use controllers. Commented Jun 20, 2016 at 8:50

2 Answers 2

1

It's undefined because it's not initialized:

var test_var;

You only set a value on the setVar function which gets called in the $scope.search_item function in the secound controller (that you never call).

What is your indented behaviour?

Sign up to request clarification or add additional context in comments.

4 Comments

all i want is to pass the item object to the second controller,,need some help on that
First of all you have to call the "search_item(...)" method. Then the getVar() method returns the set value.
when i call the "search_item(....)" methode the console.log show the object up setVar console but in getVar it is undefined in console.log
That's because the code is directly in your Controller 2 and therefore executed before the "search_item" is called. Wrapped the code in a function and call it after the search_item function!
0

You used a service, but wrote a factory.

Angular services return the function instance, so put functions on the function scope, using "this".

Using service:

app.service('var_transfer_service', function(){
    var test_var;              
    this.getVar = function () {
        return test_var;
    };
    this.setVar = function( _test_var ) {
        test_var = _test_var; 
        console.log(test_var);//show the object content in my console
    }
}   

Basically, angular service returns the function itself, but factory return the return value of the function - so, you wrote a factory.

So your code would work, if you will use app.factory:

app.factory('var_transfer_service', function(){
       var test_var;              

          return {
             getVar: function () {
                return test_var;
           },
             setVar: function( _test_var ) {
                test_var = _test_var; 
                console.log(test_var);//show the object content in my console
            }
        }
   })

4 Comments

it actually give me the same result
Does homeCtrl executes before MapCtrl? Try to set var test_var = 1 in the service, and watch if there is a change - if so, and you will see the value 1 now, than the value never gets changed.
i see the value 1, and it never changed
That means that your value never gets changed by using setVar function. There is not enough info to help from this point :/

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.