1

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

3
  • 1
    Try to observe the concept of Services in Angular. They are singletons, dedicated to that... Commented 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? Commented Aug 17, 2015 at 11:13
  • Did you found the solution from my answer? if not, then call to my number Commented Aug 17, 2015 at 11:34

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.