Best way to preload AngularJs in an Ionic app

0

I'm having a question about apps architecture.

I have an IONIC project where the app saves some basic data in the local storage so you do not have to ask for the user's login every time it is opened.

But this data is essential for the rest of the application.

I currently have the following input routes:

  • 'app.home' - > when data already exists in localstorage
  • 'app.login' - > when the data does not exist and you need to log in.

The business is that it is in the route 'app.home' that I do the verification of the localstorage and from there I decide if I stay in the route or I direct to 'app.login'. But in the 'app.home' I already need data that should be in the localstorage, and when it redirects to login and then back home, that view has already been uploaded and data saved after login does not update this view. / p>

So I thought, 'What would be the best way to do, then, does this preloader of the data and direct to login or home conform the need?'.

That's the question.

    
asked by anonymous 27.04.2016 / 19:21

1 answer

0

It is not "in the route" that you check the localStorage, it is in the controller of it. You see, you're running code that does not belong to the controller in question.

It would be best to define which route to load before entering a specific controller:

angular.module('App', [ 'ionic',...])

.run(function($state) {

     //faça sua pesquisa na localstorage aqui
     if ( ... ) {
            $state.go('app.home');
     else {
            $state.go('app.login');
     }

});
    
30.04.2016 / 04:47