Home > DeveloperSection > Forums > How to pass value one page to another page using angularjs?
Pravesh Singh
Pravesh Singh

Total Post:101

Points:709
Posted on    March-17-2015 7:17 AM

 Angular JS Angularjs Service 
Ratings:


 1 Reply(s)
 5279  View(s)
Rate this:

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>



Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    March-17-2015 7:22 AM

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


Don't want to miss updates? Please click the below button!

Follow MindStick