How to pass value one page to another page using angularjs?

In the following code I’m trying to change to another page on click and want to pass the object. How can I do it. In the following code I get it as undefined how to go about this

<a href="#/rel" ng-click="func(i)">{{i.display}}</a>
<script>
  
 
    app.controller("ctrls",['$scope','$location',function($scope,$location){
        $scope.func = function(i) {
            $scope.var=i
            $location.path("/rel");
        };
    });
 
app.controller("relctrls",'$scope',function($scope)   {
    console.log($scope.var) //undefined
});
</script>

Last updated:3/17/2015 7:22:09 AM

1 Answers

Anonymous User
Anonymous User

Pages normally have controller(s), a service can be created to share data between pages ( by injecting service in associated controllers). Like: 

app.factory('myService', function() {
 var savedData = {}
 function set(data) {
   savedData = data;
 }
 function get() {
  return savedData;
 } 
 return {
  set: set,
  get: get
 } 
});

In your controller A: 

myService.set(yourSharedData);

In your controller B: 

$scope.desiredLocation = myService.get();

Answer