4

In my angularjs page I am passing a global data to another controller using the $rootscope object of angularjs. In my controller i am successfully able to catch the value stored in $rootscope object and the value is populating correctly and i am using the value in a label contol. But when I am refreshing the page the value is getting disappeared . What is the problem in this and how cam I overcome this situation.

$scope.loginname = $rootScope.curusername;

Thanks and regards Utpal

2 Answers 2

6

If you reload your page all information that you store in JavaScript variables are lost. $rootScope is nothing more then a variable in JavaScript. If you want to store something that persists a page reload there are at least the following possibilities:

  • use a Cookie (in angular you could use the $cookieStore)
  • use some of the new HTML5 features like Locale Storage, Web DB or the old DOM Storage (http://en.wikipedia.org/wiki/Web_storage)
  • store your data on a server
4
  • @micheal I have used the technique of cookie and then retrive the value using this code $scope.getname = return $cookieStore.get("username"); then in the view written this code Use name is : {{getnames}} . But the same problem persist after page refresh the username in the getting disappeared.
    – Utpal
    Commented Jan 27, 2014 at 7:58
  • @Utpal $scope.getname is not {{getnames}}. only a typo?
    – michael
    Commented Jan 27, 2014 at 9:06
  • @micheal the actually i have taken the return value in a function. The full implementation of it is given below. $scope.getname = function () { return $cookieStore.get("username"); }
    – Utpal
    Commented Jan 27, 2014 at 9:23
  • why are you doing this in a function? i guess the function is never called. Have you tried just to write: $scope.getname = $cookieStore.get("username"); ?
    – michael
    Commented Jan 27, 2014 at 9:25
1

Ok Utpal a few things to note here:

  • AngularJS will not persist between a full page reload, you have to use another mechanism to persist data between two different loads, like HTML5 localStorage or Cookies etc. The reason why refreshing the browser deletes your data is because the entire AngularJS app is being torn down and reloaded, so can't expect it to maintain state.

  • If you want to persist data between controllers, $rootScope isn't the best way. You should look up angularJS services and use them to communicate between controllers.

Hope this helps!

3
  • I used the data service feature and reassigned the data in the Dataservice using this code and in controller retrived the data using the below code DataService.data = "Chanchal"; $scope.loginname = DataService.data; and in the view bind it like this : logeed in name2 : {{loginname}} . First time it is populating correctly but after the page refresh it is disappearing.
    – Utpal
    Commented Jan 27, 2014 at 8:02
  • the more details about my service implementation is given in the below link . Please look into this and suggest. groups.google.com/forum/?fromgroups#!topic/angular/u_HIMJOXRJs
    – Utpal
    Commented Jan 27, 2014 at 9:46
  • @Utpal you are ignoring the first point here. Where's your solution to that. Also, in your google groups link why are you returning twice from your service? I don't understand
    – Mutahhir
    Commented Jan 27, 2014 at 15:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.