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
-
1Try to observe the concept of Services in Angular. They are singletons, dedicated to that...Radim Köhler– Radim Köhler2015-08-17 09:57:20 +00:00Commented Aug 17, 2015 at 9:57
-
ya the services are helpful. But I want the user to pick a date from date picker and that has to be passed across other pages of the application. Is it possible using services?Heerapreethi P– Heerapreethi P2015-08-17 11:13:58 +00:00Commented Aug 17, 2015 at 11:13
-
Did you found the solution from my answer? if not, then call to my numberRamesh Rajendran– Ramesh Rajendran2015-08-17 11:34:29 +00:00Commented Aug 17, 2015 at 11:34
2 Answers
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
Comments
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;
});