Angularjs pass value from one page to another

2020-03-07 10:44发布

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

<button ng-href="#/page1" value="{{i.display}}"></button>

app.controller("ctrls",['$scope','$location',function($scope,$location){
  $scope.func = function(i) {
    $scope.var=i
    $location.path("/rel");
  };
]);

app.controller("ctrls",'$scope',function($scope)   {
console.log($scope.var) //undefined
]);

标签: angularjs
2条回答
够拽才男人
2楼-- · 2020-03-07 11:24

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

Happy Helping!

查看更多
闹够了就滚
3楼-- · 2020-03-07 11:24
登录 后发表回答