In Angular, how to redirect with $location.path as $http.post success callback In Angular, how to redirect with $location.path as $http.post success callback javascript javascript

In Angular, how to redirect with $location.path as $http.post success callback


Here is the changeLocation example from this article http://www.yearofmoo.com/2012/10/more-angularjs-magic-to-supercharge-your-webapp.html#apply-digest-and-phase

//be sure to inject $scope and $locationvar changeLocation = function(url, forceReload) {  $scope = $scope || angular.element(document).scope();  if(forceReload || $scope.$$phase) {    window.location = url;  }  else {    //only use this if you want to replace the history stack    //$location.path(url).replace();    //this this if you want to change the URL and add it to the history stack    $location.path(url);    $scope.$apply();  }};


There is simple answer in the official guide:

What does it not do?

It does not cause a full page reload when the browser URL is changed. To reload the page after changing the URL, use the lower-level API, $window.location.href.

Source: https://docs.angularjs.org/guide/$location


I am doing the below for page redirection(from login to home page). I have to pass the user object also to the home page. so, i am using windows localstorage.

    $http({        url:'/login/user',        method : 'POST',        headers: {            'Content-Type': 'application/json'          },        data: userData    }).success(function(loginDetails){        $scope.updLoginDetails = loginDetails;        if($scope.updLoginDetails.successful == true)            {                loginDetails.custId = $scope.updLoginDetails.customerDetails.cust_ID;                loginDetails.userName = $scope.updLoginDetails.customerDetails.cust_NM;                window.localStorage.setItem("loginDetails", JSON.stringify(loginDetails));                $window.location='/login/homepage';            }        else        alert('No access available.');    }).error(function(err,status){        alert('No access available.');          });

And it worked for me.