18

When user has enter correct username and password I want redirect to another location. But when I used $location.path('dashboard') here then URL of browser is changed but that page not loaded.when refresh I page using ctrl+R or click on refresh icon of browser then appropriate page is loaded.

$http.post('/login', $scope.userInfo)
        .success(function (data) {
            //Check if the authentication was a success or a failure
            //Successful POST here does not indicate success of authentication
            if (data.status === "authentication success") {

                //Proceed to load the dashboard for the user                    
                $location.path('/dashboard');

            } else if (data.status === "authentication error") {
                //Inform user of the error
                $scope.errorMessage = data.error;
                $scope.showErrorMessage = true;
            }

        })
        .error(function (error) {
            $scope.errorMessage = "Error while attempting to authenticate. Check  log.";
            $scope.showErrorMessage = true;

        });
    };

}]);
Chris Martin
  • 29,484
  • 8
  • 71
  • 131
Shankar Kamble
  • 2,954
  • 6
  • 23
  • 38

7 Answers7

24

Have you tried using $scope.$apply after that? eg.:

if(!$scope.$$phase) $scope.$apply()

This short method comes in handy quite often as well:

https://github.com/yearofmoo/AngularJS-Scope.SafeApply

Rafal Pastuszak
  • 3,001
  • 2
  • 28
  • 31
  • @ShankarKamble why accept the answer if it didn't help you? – Mark Amery Feb 22 '15 at 17:47
  • 4
    Please don't blindly use `$scope.$apply` without understanding why. It is rarely necessary or useful, since Angular will automatically perform your async callbacks within a digest for you. Needing apply generally means that you've stepped outside the Angular framework unnecessarily, such as by using `jQuery.ajax` instead of `$http`, `setTimeout` instead of `$timeout`, `onclick` instead of `ng-click`, or some other async callback from outside Angular that an Angular wrapper already exists for. – Mark Amery Feb 22 '15 at 17:52
  • @MarkAmery yea. I found myself here because of setTimeout. Just didn't know $timeout existed. – user137717 Sep 07 '15 at 02:55
  • @MarkAmery Very useful comment – Mr_Perfect Mar 13 '17 at 12:19
14

To quote https://docs.angularjs.org/guide/$location

"The $location service allows you to change only the URL; it does not allow you to reload the page. When you need to change the URL and reload the page or navigate to a different page, please use a lower level API, $window.location.href."

So, for example, instead of using: $location.path("/path/to/something");

Use this: $window.location.href = "/path/to/something";

$window.location.href will change the URL AND load the new page.

Hope that helps.

georgeawg
  • 47,985
  • 13
  • 70
  • 91
DondeEstaMiCulo
  • 3,341
  • 4
  • 31
  • 32
5

If you don't have $scope (like in case of service) then you can use

 $location.path('/path');
 if (!$rootScope.$$phase) $rootScope.$apply();
Aravind
  • 320
  • 2
  • 10
  • 1
    Nothing else worked for me, but this did. Thanks! :-] – Enrique Delgado Dec 02 '15 at 21:48
  • That's great. Worked perfectly for me in my error scenario. (running a 3rd party handler function from an external library inside my controller and needed to force scope). – tonejac May 20 '16 at 01:35
3

Try this (don't forget to inject $timeout):

$timeout(function () {
   $location.path('/dashboard'); 
}, 0);
Arman
  • 643
  • 5
  • 11
0

Use $timeout(function){} this will work

hazem
  • 1
  • 3
-2

Use $rootScope.$evalAsync

for example:

$rootScope.$evalAsync(function() {
     $location.path('/dashboard'); 
});
Mo.
  • 23,921
  • 35
  • 145
  • 210
Snm Maurya
  • 1,019
  • 9
  • 12
-6

Try:

 $location.path('/dashboard');
 $location.reload();
GrAnd
  • 10,051
  • 3
  • 29
  • 41