1

I am using $cookies to store userId upon successful login from a modalInstanceController and then getting the userId in my homepageController. In the whole process the state of the app is not being changed and neither the page is reloaded. Now when I console log the userId during this process it shows undefined but when I refresh the page the userId is shown perfectly.

I am pre-filling the form on successful login, so I don't want to reload my state. Any solution for this so that I can achieve that.

Code Snippet :

modalInstanceController

 $scope.login = function() {
    utilities.login()
    .success(function(response) {
      $cookies.put('user_id', response.message.user_id);
    });
    $modalInstance.dismiss('cancel');
  };

homepageController

$scope.userId = $cookies.get('user_id');

userId is only shown upon page refresh.

Simon H
  • 20,332
  • 14
  • 71
  • 128
amanpurohit
  • 1,246
  • 11
  • 19

1 Answers1

0

There is no sign in your code that you are setting userId after receiving the information back from the server. The homepageController will only read the cookie when it is loaded

 $scope.login = function() {
    utilities.login()
    .success(function(response) {
      $cookies.put('user_id', response.message.user_id);
      // add next line
      $scope.userId = $cookies.get('user_id');
    });
    $modalInstance.dismiss('cancel');
 };
Simon H
  • 20,332
  • 14
  • 71
  • 128
  • But my view is still not updating, it is being updated only after refreshing the page. – amanpurohit Nov 02 '15 at 11:35
  • Can you post more details of the the structure of your app - maybe you need to use a Service/Factory to pass data from the login component to the others – Simon H Nov 02 '15 at 11:45
  • What I am trying to achieve is to share the data globally and also retaining the data after page refresh. If I use services, the data is not persistent after page reload and when I am using `localStorageService` and `$cookies`, then I need to reload the page for reflecting the changes. – amanpurohit Nov 03 '15 at 06:16
  • A service will ensure that the data is available as you change routes – Simon H Nov 03 '15 at 08:03