How to pass data from one controller to another controller using global scope

Guys,

We will see how to pass data from one controller to another controller using global scope. I got solution from stackoverflow.

Its pretty simple..

$rootScope is a parent of all scopes. So we gonna use this to achieve solution.

My case i have 2 controllers named Controller(my first controller) and displayController(second controller). Now i want to pass an object from one controller to another controller, so that i can have a view of that object in different page.

Steps:

1. declare it in your 1str controller:

function Controller($scope, $location,$rootScope) {

2. Assign your object or variable anything to it:

$scope.user = {}; // user is my ng-model. I am getting all user.email,etc to here
$rootScope.form = $scope.user; //assigning this user to rootscope.

Hope you are aware of routing concept. If not pls find here

3. declare rootscope in second controller and assign the rootscope to normal scope variable like shown below..

app.controller(‘displayController’, function ($scope,$rootScope) {

$scope.message = ‘This is Display’;
$scope.details = $rootScope.form;
//$scope.data = Data;

});

4. Now in html page add this in body tag

<body>

We should get content here.

{{ details}} (or) {{ details.firstName}} // full details or individual attributes
</body>

You are done. Go head and test it. Enjoy šŸ™‚

Alternative: You can try this

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s