HOW TO PASS VALUE ONE PAGE TO ANOTHER PAGE USING ANGULARJS?

Pravesh Singh

Total Post:101

Points:709
Posted by  Pravesh Singh
 11779  View(s)
Ratings:
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>

  1. Ankita Pandey

    Post:183

    Points:1285
    Re: How to pass value one page to another page using angularjs?

    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

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!