简体   繁体   中英

$location.path() not working correct

this code changing path when user not authorized, but view update only after page reload. $state.reload() and $rootScope.apply() don't help.

$rootScope.$on('$stateChangeStart', function (event, next) {
  if (!$localStorage.authenticate) {
        $rootScope.authenticate = false;

     if (next.url == '/reset_password/:token') {
          console.log('reset password page');
        }
        else if (next.url == '/forgot_password') {
          $location.path(next.url);

        }
        else {
          $location.path('/login');
        }
      }
      else {
        $rootScope.authenticate = true;
        if ((next.url == '/login') || (next.url == '/forgot') || (next.url == '/reset_password/:token')) {
          $location.path('/index');


        }
        $rootScope.userName = $localStorage.userName;
        API.getService('/login').get(function (res) {
          if (!res.logged) {
            $location.path('/login');
            delete $localStorage.userName;
            $localStorage.authenticate = false;
          }
        })
      }
    });

In ui router you should not use $location

To change state you have to use this :

 $state.go("mystatename", {urlParam:value});

In your HTML don't use href, use ui-sref instead :

 ui-sref = "mystatename({urlParam:value})"

Hope it solved your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM