How to keep $stateparams parameters after i refres

2019-07-27 04:20发布

问题:

I have this list of users .. When a user clicks in one of those user in the list.. it will redirect me to my profileState with the associated UID of the user. I use state parameters to pass the UID from the userListState to the profileState.. When i type

console.log($stateParams);

it displays the UID which i passed from userListState .. but once i refresh the page.. the UID will become null.. So i would like to know how you guys make that specific data remain there after page reload.

回答1:

How do you have configured your states? becuase you should do something like

.state('app.profile', {
    url: '/profile/:uid',
    views: {
        '@app': {
            templateUrl: 'app/profile/index.html',
            controller: 'ProfileController as profileCtrl'
        }
    },
    params: {
        uid: null
    }
})

this way when you go to your state you'll end up with localhost/profile/234

you will refresh and uid will be there.



回答2:

There are 2 common ways to store your uid:

  1. Assign variables to $rootScope, that way its visible to all the controllers

  2. localStorageService , this will hold the values even after user refreshes the page.