添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I store an authentication token in a localStorage variable window.localStorage.getItem("token");

I query this variable in the `$ionicPlatform.ready(function() { }); and if I don’t set a time out of 1500, the variable isn’t found

if(window.localStorage.getItem("token") != null){
    // Never true
setTimeout(function(){
    if(window.localStorage.getItem("token") != null){
        // True
},1500);

I’m not sure what the “proper” way is to get around this, is there a “local storage ready” broadcast event? I’m not storing anything that complex in local storage (a few arrays), so I don’t believe that is causing the hold up.

Any pointers?

Cheers
-Chris

I’m looking to solve a similar problem.
I guess what we are looking for is some way to “halt” the app to continue loading until the localstorage data has been set?

My problem is that I add content from a .json file that contains several thousand objects…

Yepp… I have it like this right now:

.run(function($ionicPlatform, $localForage, $http) {
  $ionicPlatform.ready(function() {
    // Get names and save in localStorage
    $http.get('js/names.json').then(function(names) {
      $localForage.setItem('names', names.data).then(function(data) {
        console.log('Store all names in localStorage');
        //console.log(data);
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleLightContent();
              

I have found differences between device ready and ionic ready. It may be worth specifically trying device ready.

In addition, I don’t know if the issue is that you’re not getting the names from the JSON file in time. Would you not need a promise on that so that it try to do the setItem until it has completed the http.get?

Have you console logged between the get and the set to check that the file is fully read in before it tries to set them into local storage?

In my console I see that the directive is calling the get function before the set is called…?
Should that be the case when I have the set function in .run block?

Great news on both counts.

Hopefully one of the main ionic team or someone else with a better understanding will be able to explain why device ready reacts differently to ionic ready. I’ve spent so much time on this and hundreds of other issues over the last year that I have forgotten the specifics. :slight_smile:

Anyway, glad I could help.