Heerapreethi P
Heerapreethi P

Reputation: 49

How to pass date value from one controller to another in angular ui routing

I want to pass the date value which the user selects in one page to another page using angular UI routing. There is no parent child relationship between these controllers.Thanks in advance

Upvotes: 1

Views: 850

Answers (2)

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38663

Variables set at the root-scope are available to the controller scope via prototypical inheritance.

Every application has a single root scope. All other scopes are descendant scopes of the root scope. Scopes provide separation between the model and the view, via a mechanism for watching the model for changes. They also provide an event emission/broadcast and subscription facility

Sample

angular.module('myApp', [])   
.controller('myCtrl', function($scope, $rootScope) {

       $rootScope.myCtrlVariable = new Date();// Your date-picker selected Date 

})
.controller('myCtrl2', function($scope, $rootScope) {

   $scope.myCtrl2Variable = $rootScope.myCtrlVariable;
});

Upvotes: 0

Joe Lloyd
Joe Lloyd

Reputation: 22323

Best Practice

You should create a service with setters and getters for the data that you want to share, you can then instantiate these services in the controllers you wish to share data between.

This lets you safely move data between the two.

Alternative

If you want a quick and dirty way too share data between controllers then use $rootScope, it acts as a global variable but remember that there are problems with these variables see here

Upvotes: 1

Related Questions