1

I have two controllers FundsController and FundRequestContoller. FundRequestController is used to show fund request received to an user. And FundsController contains a form which has an option to select on of my friend and amount that a user wish to send to his friend.

And, when ever a user accept a particular fund request by a friend, a state transition should happen to navigate the user from FundRequestController view to FundsController view where the friend id and amount to requested be pre-populated.

I would like to create a json object and share this object with FundsController so that when it is initialized the friend id and amount can be pre-populated using json object.

I dont want to use query params since too much information will be displayed to user in the browser url.

How can I share a json objects from one controller to another controller that can be used to initialize the other controller??

raghu
  • 59
  • 1
  • 9

2 Answers2

2

Use factory to create your service which holds the shared data. This service is then injected into both of your controllers as a dependency.

app.factory("sharedDataService", function() { return { yourData: "whatever" }; });

see this :

Community
  • 1
  • 1
PeterFromCologne
  • 10,213
  • 9
  • 36
  • 46
0

Best approach is to make factory file as common to both controllers .

More Info

http://plnkr.co/edit/UA28cXfF0I8r3M54DyYa?p=preview

Another simple method is that make one function as global and share data

Controller 1

$scope.shareData( data ) = function(){

  $scope.sampleFunction( data) ;

  };

Controller 2

$rootScope.sampleFunction( data ) = function(){

  console.log(data);
  // your logic

  };

More Info

Prashobh
  • 9,216
  • 15
  • 61
  • 91